Basic usage of Unity
1. Create an unitycontainer object.
2. Use the registertype method of the unitycontainer object to register the relationship between the object and the object.
3. Use the Resolve Method of the unitycontainer object to obtain the object associated with the specified object.
Using system; using system. collections. generic; using system. LINQ; using system. text; using Microsoft. practices. unity; using Microsoft. practices. unity. configuration; namespace unitydemo {class program {static void main (string [] ARGs) {iunitycontainer Container = new unitycontainer (); container. registertype <ilogger, consolelogger> (); ilogger logger = container. resolve <ilogger> (); logger. log ("Hello World"); console. readline ();}}}
You can use registertype in either of the following ways:Code, Similar to ontainer. registertype <ilogger, consolelogger> ();
One is through the configuration file
You need to reference the unity. configuration class library in the configuration file. To avoid XML input errors, you can enable smart sensing through the XSD file in either of the following ways:
Method 1: select the XML tab in Visual Studio, select the schemas option, find the unityconfigurationconfigurationconfigurxsd file in it, and select use
Method 2, enter <unity xmlns = "directly in the XML configuration file, and then select the http://schemas.microsoft.com/practices/2010/unity in the pop-up prompt
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <configsections> <section name = "Unity" type = "Microsoft. practices. unity. configuration. unityconfigurationsection, Microsoft. practices. unity. configuration "/> </configsections> <unity xmlns =" http://schemas.microsoft.com/practices/2010/unity "> <container> <register type =" unitydemo. ilogger, unitydemo "mapto =" unitydemo. filelogger, unitydemo "> </register> </container> </Unity> </configuration>
If there are too many configurations, you can create a single unity. config.
Using system; using system. collections. generic; using system. LINQ; using system. text; using Microsoft. practices. unity; using Microsoft. practices. unity. configuration; using system. configuration; namespace unitydemo {class program {static void main (string [] ARGs) {iunitycontainer Container = new unitycontainer (); unityconfigurationsection section = getunityconfigurationsection ("unity. config "); container. loadconfiguration (); ilogger logger = container. resolve <ilogger> (); logger. log ("Hello World"); console. readline ();} public static unityconfigurationsection getunityconfigurationsection (string configfile) {var filemap = new execonfigurationfilemap {execonfigfilename = httpcontext. current. server. mappath (configfile)}; configuration = configurationmanager. openmappedexeconfiguration (filemap, configurationuserlevel. none); Return (unityconfigurationsection) configuration. getsection ("Unity ");}}}
In the configuration file, the type must be written and formed, such as:"Namespace. typename, assemblyname. This makes the configuration file very bloated and difficult to maintain. InUnityProvidesAutomatic type lookupTo solve this problem.
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <configsections> <section name = "Unity" type = "Microsoft. practices. unity. configuration. unityconfigurationsection, Microsoft. practices. unity. configuration "/> </configsections> <unity xmlns =" http://schemas.microsoft.com/practices/2010/unity "> <namespace name =" unitydemo "/> <Assembly name =" unitydemo "/> <container> <Register Type = "ilogger" mapto = "filelogger"> </register> </container> </Unity> </configuration>