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