Original article: http://www.doriandeng.cn/archives/95.html.
Unity is a lightweight and scalable dependency injection (DI) Container developed by Microsoft. It supports constructor, attribute, and method dependency injection. The so-called dependency injection is coming soonProgramThe objects in the development process are decoupled from the specific objects they depend on. To a certain extent, they only focus on the use of the dependent objects, regardless of the type of the object; the dependent objects are injected by containers at runtime. Essentially, it is an object factory and Object Manager. For more information, see IOC container and dependency injection mode.
Unity is applicable to the Development of extensible and pluggable applications. Development of common applications that do not require plug-ins increases the development complexity. The use of unity also has a small impact on performance.
Preparation
In this seriesArticleAssume that there is a telephone class, an idialer interface, and a dialer abstract class. As follows:CodeAs shown in:
Unity Initialization
Before using the unity container, you generally need to initialize the object type in use. In some cases, you can skip initialization. The initialization of the Unity container is mainly the registration of object types. This can be done in two ways. One is to use. Net code, and the other is to use xml configuration files. In the examples in the Unity document, most examples Use code to initialize Untiy, this also gives many people the illusion that using unity containers to manage objects is more complex than using them directly. This article will also describe initialization and other related aspects using code and xml configuration files, so that you can have a more comprehensive and correct understanding of unity.
Type Registration
First, add a reference to the Microsoft. Practices. Unity namespace in the code file.
Then, you can perform the following registration using code (the buttontypedialer class is included inSource code.) :
The running result is as follows:
Next, let's take a look at how to initialize with XML.
First, add the following configuration section declaration in the configuration file:
Then, configure the type registration:
Finally, we can use the following methods:
From the code above, we can see that the methods used after the unity container initialization are the same.
Constructor Injection
Essentially, the above example can be implemented using the following code:
The only difference between the two paragraphs is that 38 and 39 rows are merged into 52 rows. This is the use of constructor injection.
In the telephone class, we declare a constructor of public telephone (idialer dialer). In the unity container, we have registered the constructor for the idialer interface, therefore, when the Resolve Method of the Unity container is used to obtain the object, this constructor is automatically called to initialize the object. If such a constructor does not exist, the Unity container automatically calls its default constructor for initialization.
If multiple constructors with parameters exist at the same time, you must use the [injectionconstructor] feature to specify the constructors to be used.
Property Injection
To use property injection, we need to add the [dependency] attribute declaration to the dialer attribute of the telephone class, as shown below:
In addition, add the following configuration in the configuration file to register the ing of the dialer abstract class:
In this way, the sample code in constructor injection is still valid, although the constructor has been commented out.
Method call Injection
We will further modify the telephone class, comment out the [dependency] feature, and add an initialize method:
In the initialize method, we add [injectionmethod] to indicate that the injection method is called using a method. This also makes the sample code used in constructor injection still valid.
Summary
Unity is a lightweight and scalable di container that can be registered using code or configuration files. It also supports constructor injection, property injection, and method call injection.
Source code download
Look-into-unity-1-x.zip
Ideal & Beauty