Unity-IOC framework data sorting

Source: Internet
Author: User

Today, I found some unity materials on the Internet and found many problems. Compilation failed. After some guidance, I finally compiled and ran it. I made a note as follows:

1. Download unity;

2. Add Microsoft. Practices. Unity. dll and Microsoft. Practices. Unity. Configuration. dll references to the project;

3. app. config is 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  xmlns="http://schemas.microsoft.com/practices/2010/unity">    <alias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,Microsoft.Practices.Unity"></alias>    <alias alias="ISayHello2" type="TestUnity.ISayHello2,TestUnity"></alias>    <alias alias="SayHello2Think" type="TestUnity.SayHello2Think,TestUnity"></alias>    <alias alias="SayHello2ACloud" type="TestUnity.SayHello2ACloud,TestUnity"></alias>    <alias alias="ISingletonDemo" type="TestUnity.ISingletonDemo,TestUnity"></alias>    <alias alias="SingletonDemoImp1" type="TestUnity.SingletonDemoImp1,TestUnity"></alias>    <container name="TestUnity">      <register type="ISayHello2" mapTo="SayHello2Think" name="think"/>      <register type="ISayHello2" mapTo="SayHello2ACloud" name="aCloud"/>      <register type="ISingletonDemo" mapTo="SingletonDemoImp1" name="singletonDemoImp1">      </register>    </container>  </unity></configuration>

The Configuration section of Unity is named "Unity". The type of the section handler is Microsoft. Practices. Unity. Configuration. UnityConfigurationSection, which is included in the Assembly Microsoft. Practices. Unity. Configuration;

Name: the name used when registering this type. This attribute is optional. If this attribute is not specified, the add element is the default type ing.

Type: The Source type configured in the container. If this is ing registration, this is the type of the starting object of the ing; if this is a single-piece registration, this is the type of the object. This attribute is required.

MapTo: Target type of Type ing. If this is ing registration, this is the type of the mapped target object. This attribute is optional.

Lifetime: Set the lifecycle of a given type and name. Is a value from the LifetimeStyle enumeration. The valid value is Transient (default), which causes the container to create a new instance each time; and Singleton, which causes the container to return the same instance for each request. If both the type and mapto attributes are specified when a single piece is configured, The SetSingleton method returns the type specified in the mapTo attribute. If no value is specified for the mapTo attribute, the SetSingleton method returns the type specified in the type attribute.

4. Create a project and add the following files to it:

ISayHello2.cs

Public interface ISayHello2
{
String SayHello ();
}

 

SayHello2Think8848. cs

Public class SayHello2Think8848: ISayHello2
{
# Region ISayHello2 Member

Public string SayHello ()
{
Return "Hello think8848! ";
}

# Endregion
}

 

SayHello2ACloud. cs

Public class SayHello2ACloud: ISayHello2
{
# Region ISayHello2 Member

Public string SayHello ()
{
Return "Hello aCloud! ";
}

# Endregion
}

 

ISingletonDemo. cs

Public interface ISingletonDemo
{
DateTime GetInitTime ();
}

 

SingletonDemoImpl. cs

Public class SingletonDemoImpl: ISingletonDemo
{
Private DateTime time;

Public SingletonDemoImpl ()
{
This. time = DateTime. Now;
}

# Region ISingletonDemo Member

Public DateTime GetInitTime ()
{
Return this. time;
}

# Endregion
}

 

5. The Main method is as follows:

 static void Main()  {             UnityContainer myContainer=new UnityContainer();            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");            section.Configure(myContainer, "TestUnity");         //   section.Containers["TestUnity"].Configure(myContainer);            ISayHello2 sayHello2ACloud = myContainer.Resolve<ISayHello2>("aCloud");            Console.WriteLine(sayHello2ACloud.sayHello());}

  

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.