Basic usage of Unity

Source: Internet
Author: User

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>

 

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.