Summarize the basic usage methods of the unity IOC container to implement type mapping by configuration

Source: Internet
Author: User

There are a lot of ways to use unity IOC containers online, but not a summary, so here is a summary, convenient for everyone to choose.

Let's start with the code for type mapping, as simple as the code:

UnityContainer = new UnityContainer (); Instantiate a container unitycontainer.registertype<iclasstest, classtest> (); Register type mapping unitycontainer.resolve<iclasstest> ();//parse and get instance of type

However, the actual situation, we generally can not directly hard code in the program, because like the above code and ordinary direct new An instance of no difference, there will be obvious dependence, if you need to change the class, you must modify the program source code, then lost the role of the IOC, So most of the use of external configuration files to achieve automatic mapping, the following is my summary of several configuration methods (strictly speaking, or a kind), for everyone to choose.

The first configuration is as follows:

  <configSections> <section name= "Unity" type= " Microsoft.practices.unity.configuration.unityconfigurationsection,microsoft.practices.unity.configuration "/ > </configSections> <unity> <typeAliases> <typealias alias= "irepository ' 1" type= "Consol EApplication1.DDD.Interface.IRepository ' 1,consoleapplication1 "/> <typealias alias=" Post "type=" Consoleapplication1.entities.post,consoleapplication1 "/> <typealias alias=" Category "type=" Consoleapplication1.entities.category,consoleapplication1 "/> <typealias alias=" Author "type=" Consoleapplication1.entities.author,consoleapplication1 "/> <typealias alias=" IClassTest "type="      Consoleapplication1.ddd.interface.iclasstest,consoleapplication1 "/> </typeAliases> <container> <types> <type type= "Irepository[post" "mapto=" ConsoleApplication1.DDD.Infrastructure.Repositories.PostRe        Pository,consoleapplication1 "/><type type= "Irepository[category" "mapto=" Consoleapplication1.ddd.infrastructure.repositories.categoryrepository,consoleapplication1 "/> <type type=" Irepository[author] "mapto=" ConsoleApplication1.DDD.Infrastructure.Repositories.AuthorRepository, ConsoleApplication1 "/> <type type=" iclasstest "mapto=" consoleapplication1.ddd.interface.classtest,consoleap Plication1 "/> </types> </container> </unity>

Note that the [] square brackets in the configuration represent the generic parameters, the type in Typealiases->typealias and the Mapto in Types->type require the full qualified name of the configuration type and the assembly where the type resides, separated by commas. For example: ConsoleApplication1.DDD.Interface.IRepository ' 1 represents a irepository<t> generic type, And ConsoleApplication1 represents the assembly where this generic is located, not the namespace!

The second configuration is as follows:

  <configSections> <section name= "Unity" type= " Microsoft.practices.unity.configuration.unityconfigurationsection,microsoft.practices.unity.configuration "/ > </configSections> <unity> <aliases> <add alias= "irepository ' 1" type= "Consoleapplicatio N1. Ddd. Interface.irepository ' 1,consoleapplication1 "/> <add alias=" Post "type=" ConsoleApplication1.Entities.Post, ConsoleApplication1 "/> <add alias=" Category "type=" ConsoleApplication1.Entities.Category, ConsoleApplication1 "/> <add alias=" Author "type=" Consoleapplication1.entities.author,consoleapplication1 "/&      Gt <add alias= "Iclasstest" type= "Consoleapplication1.ddd.interface.iclasstest,consoleapplication1"/> </ aliases> <container> <register type= "irepository[post]" mapto= "ConsoleApplication1.DDD.Infrastructure . Repositories.postrepository,consoleapplication1 "/> <register type=" irepository[category] "mapTo=" ConsoleAPplication1. Ddd.  Infrastructure.repositories.categoryrepository,consoleapplication1 "/> <register type=" IRepository[Author] " mapto= "Consoleapplication1.ddd.infrastructure.repositories.authorrepository,consoleapplication1"/> < Register type= "Iclasstest" mapto= "Consoleapplication1.ddd.interface.classtest,consoleapplication1" ></ Register> </container> </unity>

The third configuration is as follows (in fact, the shorthand for the above two types):

  <configSections>    <section name= "Unity" type= " Microsoft.practices.unity.configuration.unityconfigurationsection,microsoft.practices.unity.configuration "/ >  </configSections>  <unity>    <assembly name= "ConsoleApplication1" ></assembly >    <namespace name= "consoleapplication1.entities" ></namespace>    <namespace name= " ConsoleApplication1.DDD.Interface "></namespace>    <namespace name=" ConsoleApplication1.DDD.Infrastructure.Repositories "></namespace>    <container>      < Register type= "Irepository[post" "  mapto=" postrepository "/>      <register type=" Irepository[category] "  mapto=" categoryrepository "/>      <register type=" Irepository[author] "  mapto=" Authorrepository "/>      <register type=" iclasstest "mapto=" classtest "  ></register>    </container>  </unity>

How to use the code:

UnityContainer = new UnityContainer (); unitycontainer.loadconfiguration ()//If the name of the container node is specified in the configuration, You need to specify the name unitycontainer.resolve<irepository<post>> ();//Parse and instantiate an object//the following is a separate configuration file (e.g., unity.config) How to use:            iunitycontainer container = new UnityContainer ();            String configfile = "Unity.config";            var filemap = new Execonfigurationfilemap {execonfigfilename = configfile};            Reading configuration information from config file configurations            =configurationmanager.openmappedexeconfiguration (Filemap, Configurationuserlevel.none);            Gets the configuration section for the specified name,            unityconfigurationsection sections = (unityconfigurationsection) configurationmanager.getsection (" Unity ");            Load the container node container named FirstClass            . Loadconfiguration (section, "MyContainer");

From the content of the configuration, a look at the third kind of configuration comparison is efficient and concise, the implementation of the principle is also very simple, is to map the relevant types of assembly and the namespace is defined in the Unity node first, and then you can directly configure the registration map, of course, the above configuration is simple, Does not contain other properties, such as configuration lifetime, constructor, etc., these need to be based on the actual situation to configure, do not explain here, you can refer to the online other cattle tutorials.

Summarize the basic usage methods of the unity IOC container to implement type mapping by 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.