Microsoft dependency injection Unity, Microsoft injection unity

Source: Internet
Author: User

Microsoft dependency injection Unity, Microsoft injection unity
Unity is a Dependency Injection (DI) container. DI's standard description article comes from Martin Flower [0]. As a fast summary, dependency injection containers are a tool for building highly loosely coupled software. The dependency injection container handles all the details of the interconnected objects, so you can build an independent component.

This is an encyclopedia of dependency injection: http://baike.baidu.com/view/1800021.htm.

This is the address of unity: http://unity.codeplex.com/
Download the latest version 2.1, http://www.microsoft.com/download/en/details.aspx? Id = 17866
The download is an msi file. After installation, there are bin folders and source code and several instance projects.

Open vs, create a project, and add a reference to Microsoft. Practices. Unity. dll to use dependency injection.

1. Prepare interfaces and Classes
Public interface ILog
{
Void Write (string message );
}

Public class TextFileLogger: ILog
{
Public void Write (string message)
{
Console. WriteLine ("Text File Write" + message );
}
}

Public class DatabaseLogger: ILog
{
Public void Write (string message)
{
Console. WriteLine ("Database Write" + message );
}
}

Code-based dependency Injection
2. Create a container in the static void Main (string [] args) method.
Containers can register interfaces and return interfaces, which are the core of dependency injection.

Simple Steps: 1. Create a container, 2. register the interface ing, and 3. Obtain the instance.

// Container
IUnityContainer parentContainer = new UnityContainer ();
// Parent container can create sub-containers
// IUnityContainer chileContainer = parentContainer. CreateChildContainer ();
// Default object
ParentContainer. RegisterType <ILog, UnityDI. TextFileLogger> ();
// Give the datalogger a name in the container and obtain the databaselogger according to the name.
ParentContainer. RegisterType <ILog, UnityDI. DatabaseLogger> ("database ");

// If no parameter exists, the default value is used.
ILog textLog = parentContainer. Resolve <ILog> ();
TextLog. Write ("aaaaaaaaaaa ");
// Go by name
ILog dataLog = parentContainer. Resolve <ILog> ("database ");
DataLog. Write ("aaaaaaaaaaa ");

// Print out registered ILogger interface objects. default parameters are not output.
Foreach (object mapping in parentContainer. ResolveAll <ILog> ())
{
Console. WriteLine (mapping. GetType ());
}

Enter dependency injection according to the configuration file
In the config file
Configure <configSections>
<Section name = "unity" type = "Microsoft. Practices. Unity. Configuration. UnityConfigurationSection, Microsoft. Practices. Unity. Configuration"/>
</ConfigSections>
(Note that the configSections node must be the first node in configuration)

Then

<Unity>
<TypeAliases>
<! -- Three different types of lifecycles -->
<TypeAlias alias = "singleton"
Type = "Microsoft. Practices. Unity. ContainerControlledLifetimeManager, Microsoft. Practices. Unity"/>
<TypeAlias alias = "external"
Type = "Microsoft. Practices. Unity. ExternallyControlledLifetimeManager, Microsoft. Practices. Unity"/>
<TypeAlias alias = "perThread"
Type = "Microsoft. Practices. Unity. PerThreadLifetimeManager, Microsoft. Practices. Unity"/>

<! -- Register an alias for the class. You can directly use the alias to replace the specific class. type = specifies the detailed namespace and class Noun of the class, and the comma is followed by the Assembly noun. -->
<TypeAlias alias = "DatabaseLogger" type = "leleapplication1.unitydi. DatabaseLogger, ConsoleApplication1"/>
<TypeAlias alias = "TextFileLogger" type = "leleapplication1.unitydi. TextFileLogger, leleapplication1"/>
<TypeAlias alias = "ILogger" type = "leleapplication1.unitydi. ILog, ConsoleApplication1"/>
</TypeAliases>
<Containers>
<Container>
<Types>
<! -- Type is the interface, and mapto is the object of target instantiation -->
<Type = "ILogger" mapTo = "DatabaseLogger">
<Lifetime type = "singleton"/>
</Type>

</Types>
</Container>
</Containers>
</Unity>
Add Microsoft. Practices. Unity. Configuration. dll
Then you can use the code.Instantiate different interface objects through different mapto in the configuration file
IUnityContainer container = new UnityContainer ();

UnityConfigurationSection config = ConfigurationManager. GetSection ("unity") as UnityConfigurationSection;
Config. Configure (container );

ILog defaultLogger = container. Resolve <ILog> ();
DefaultLogger. Write ("hello Unity! ");


The Microsoft Enterprise Library 5.0 manual contains the unity documentation, which does not seem to be found anywhere else.
Http://www.microsoft.com/download/en/details.aspx? Displaylang = en & id = 6932

Other msdn materials http://msdn.microsoft.com/en-us/library/ff660923%28v=PandP.20%29.aspx

My Sina Weibo: http://weibo.com/ I /1741159542

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.