. NET Ico Unity for just getting started

Source: Internet
Author: User
Tags aliases

Introduced:

To begin with, unity is a lightweight, extensible dependency injection container implemented by the Microsoft patterns& Practices group in C # that configures the relationships between objects through code or XML configuration files. Then it is implemented by a simple code.

Test Project Details

1. Create a new console project called Unitydemo

2. Create an interface iproduct, including a property (string Classname{get;set;} ), a method (void Showinfo ())

3, establish two class Milk,sugar, and inherit iproduct, two class implementation code as follows:

public class Milk:iproduct    {public        string ClassName {get; set;}        public void Showinfo ()        {            Console.WriteLine ("Milk:{0}", ClassName);        }    }

  

public class Sugar:iproduct    {public        string ClassName {get; set;}        public void Showinfo ()        {            Console.WriteLine ("Sugar:{0}", ClassName);        }     }

  

Start testing

1. Use Unity to reference three DLLs, or you can use NuGet to get

2, as mentioned earlier, there are two ways to use unity, we first use code to control

A, create a new static method Contaninercode, you need to refer to the Microsoft.Practices.Unity code as follows:

static void Containercode () {//Create unity instance iunitycontainer container = new UnityContainer (); Default registration (unnamed) container.            Registertype<iproduct, milk> (); Name Registration container.            Registertype<iproduct, sugar> ("Sugar"); Resolves the default object iproduct _product = container.            Resolve<iproduct> (); _product. ClassName = _product. GetType ().            ToString (); _product.            Showinfo (); Specifies the named resolution object IProduct _sugar = container.            Resolve<iproduct> ("Sugar"); _sugar.classname = _sugar.gettype ().            ToString ();            _sugar.showinfo (); Gets all the iproduct registered named objects in the container (not including the default registration) ienumerable<iproduct> Classlist = container.            Resolveall<iproduct> (); foreach (var item in classlist) {item. ClassName = Item. GetType ().                ToString (); Item.            Showinfo (); }        }

The results of the operation are as follows:

3, using the configuration file implementation

A, the root directory new Unity.config, the property selection copy to the build directory , the configuration file contents are as follows:

<?xml version= "1.0" encoding= "Utf-8"?><configuration> < configsections> <section name= "Unity" type= " Microsoft.practices.unity.configuration.unityconfigurationsection,microsoft.practices.unity.configuration "/ > </configSections> <unity> <!--define aliases--<aliases> <add alias= "IProduct" type= "Unitydemo.iproduct,unitydemo"/> <add alias= "Milk" type= "Unitydemo.milk,unitydemo"/> <add alias= "Sug Ar "type=" Unitydemo.sugar,unitydemo "/> </aliases> <!--containers--<container name=" MyContainer " > <!--start mapping relationships--<register type= "iproduct" mapto= "Milk" ></register> <register Type= "IProduct" mapto= "Sugar" name= "Sugar" ></register> </container> </unity> </config Uration> 

b, to create a static method Containercodeforconfig () load the configuration file, you need to refer to Dll,practices.unity.configuration, System.Configuration code as follows:

Static voidContainercodeforconfig () {//Create a Unity instanceIunitycontainer container =NewUnityContainer (); stringConfigFile ="Unity.config"; varFilemap =NewExeconfigurationfilemap {execonfigfilename =ConfigFile}; //read the specified config fileConfiguration Configuration =configurationmanager.openmappedexeconfiguration (Filemap, Configurationuserlevel.none); //gets the node with the specified nameUnityconfigurationsection section = (unityconfigurationsection) configuration. GetSection ("Unity"); //Get Container nodeContainer. Loadconfiguration (section,"MyContainer"); IProduct P= Container. Resolve<iproduct> ("Sugar"); P.classname=P.gettype ().            ToString ();        P.showinfo (); }

The results of the operation are as follows:

You can see that we didn't create the iproduct, sugar class, but we can call its method, which is what the IOC did for us.

This is a very simple introduction.

. NET Ico Unity for just getting started

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.