About dependency injection container unity

Source: Internet
Author: User
Document directory
  • 1. Create a container
  • 2. Registration ing
  • 3. Get object instances

Unity is a lightweight and scalable dependency injection container developed by the Microsoft team. It provides a good solution for loosely coupled applications and supports constructor injection, property injection, and method injection.


Similarly, the IOC and dependency injection examples in Di are reversed based on the control.

Interface idal <br/>{< br/> void save (); <br/>}< br/> class sqlserverdal: idal <br/>{< br/> Public void save () <br/>{< br/> console. writeline ("sqlserver save. "); <br/>}< br/> class implements ledal: idal <br/>{< br/> Public void save () <br/> {<br/> console. writeline ("Oracle save. "); <br/>}< br/>}

The implementation of unity is as follows:

Iunitycontainer Container = new unitycontainer (); <br/> container. registertype <idal, incluledal> (); <br/> var Dal = container. resolve <idal> (); // This is the instance for which the ledal is obtained. <Br/> Dal. Save (); <br/>

1. Create a container Iunitycontainer Container = new unitycontainer ();2. Register ing a) register ing in code mode: Container. registertype <idal, incluledal> ();
Registertype has the following overload methods:
Registertype <tfrom, role> (
)
Registertype <tfrom, memory> (lifetimemanager lifetime)
Registertype <tfrom, struct> (string name)
Registertype <tfrom, struct> (string name,
Lifetimemanager lifetime)
Registertype <t> (lifetimemanager lifetime)
Registertype <t> (string name,
Lifetimemanager lifetime)
Registertype (type from,
Type)
Registertype (type from,
Type,
String name)
Registertype (type from,
Type,
Lifetimemanager lifetime)
Registertype (type from,
Type,
String name,
Lifetimemanager lifetime)
Registertype (type T,
Lifetimemanager lifetime)
Registertype (type T,
String name,
Lifetimemanager lifetime)

B) register the ing through Configuration:

Unityconfigurationsection section = (unityconfigurationsection) configurationmanager. getsection ("Unity "); The configuration of the config file is as follows: <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <configuration> <br/> <configsections> <br/> <section name = "Unity" <br/> type = "Microsoft. practices. unity. configuration. unityconfigurationsection, <br/> Microsoft. practices. unity. configuration "/> <br/> </configsections> <br/> <unity> <br/> <containers> <br/> <container> <br/> <types> <br/> <type <br/> type = "unitydemo. idal, unitydemo "<br/> mapto =" unitydemo. oracledal, unitydemo "/> <br/> </types> <br/> </container> <br/> </containers> <br/> </Unity> <br/> </configuration>

Complete configuration method code:

Iunitycontainer Container = new unitycontainer (); <br/> unityconfigurationsection section = (unityconfigurationsection) configurationmanager. getsection ("Unity"); <br/> section. configure (container); <br/> var Dal = container. resolve <idal> (); // This is the instance for which the ledal is obtained. <Br/> Dal. Save ();

3. Get object instancesVaR Dal = container. Resolve <idal> (); // This is the instance that obtains the oracledal.You can use resolveall to obtain instances of all registered objects:<Types> <br/> <type <br/> type = "unitydemo. idal, unitydemo "name =" oracle "<br/> mapto =" unitydemo. incluledal, unitydemo "/> <br/> <type <br/> type =" unitydemo. idal, unitydemo "name =" sqlserver "<br/> mapto =" unitydemo. sqlserverdal, unitydemo "/> <br/> </types>VaR Dals = container. resolveall <idal> (); <br/> foreach (idal Dal in Dals) <br/>{< br/> Dal. save (); <br/>}
Result:

Reference:

Http://unity.codeplex.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.