The so-called control reversal (ioc:inversion of controls) simply means that the application itself is not responsible for the creation and maintenance of dependent objects and is entrusted to an external container. In this way, the control is transferred from the application to the external IOC container, and the control power realizes the so-called inversion. For example, an instance of type B is required in type A, and the creation of a B instance is not owned by a, but is created by an external container. The way through IOC is to realize the activation of target controller is of great significance.
One, from unity to understand the IOC
Sometimes we call the IOC Dependency injection (di:dependency injection). The so-called dependency injection is that the external container dynamically injects dependent objects into the component at runtime. Martin Fowler, in the famous article "Inversion of control containers and the Dependency injection", divides specific dependency injection into three forms, namely, constructor injection, Attribute (set) injection and interface injection, which I have personally used to classify as one (type) match and three kinds of injection:
Type Matching: Although we make service calls through interfaces (or abstract classes), the service itself is implemented in a specific service type, which requires a type registration mechanism to resolve the matching relationship between the service interface and the service type;
Constructor injection (constructor injection): The IOC container intelligently chooses to select and invoke the appropriate constructors to create dependent objects. If the selected constructor has the corresponding parameters, the IOC container resolves the registered dependencies and obtains the corresponding parameter object by itself before calling the constructor;
Property injection: If you need to use a property of a dependent object, the IOC container automatically initializes the property after the dependent object is created;
Method injection: If a dependent object needs to invoke a method for the appropriate initialization, the IOC container automatically invokes the method after the object is created.
The open source community has a very popular IOC framework, such as Castle Windsor, Unity, Spring.net, StructureMap and Ninject. Unity is a lightweight IOC framework developed by Microsoft's patterns & practices department. The item on the CodePlex address is http://unity.codeplex.com/, you can download the appropriate installation package and development documentation. The latest version of Unity is 2.1. It is impossible for me to introduce unity for the sake of space, but in order for the reader to understand the IOC's implementation in unity, I have written a simple procedure.
We create a console program that defines the following interfaces (IA, IB, IC, and ID) and their respective implementation classes (A, B, C, D). 3 Properties B, C, and d are defined in type A, which are the interface IB, IC, and ID respectively. where attribute B is initialized in a function, thinking that it will be initialized as a constructor injection; The Dependencyattribute attribute is applied on attribute C, which means that it is a dependency property that needs to be initialized as a property injection ; Property D is initialized by method initialize, which applies attribute injectionmethodattribute, meaning that this is an injection method that is automatically invoked when a object is created by the IOC container.
1:namespace Unitydemo
2: {
3: Public interface IA {}
4: Public interface IB {}
5: Public interface IC {}
6: Public interface ID {}
7:
8: Public class A:ia
9: {
Public IB B {get; set;}
One: [Dependency]
: Public IC C {get; set;}
Public ID D {get; set;}
14:
: Public A (IB b)
: {
A: this . b = b;
: }
: [Injectionmethod]
: Public void Initialize (ID D)
: {
All: this . D = D;
: }
: }
: Public class b:ib{}
Num: Public class c:ic{}
: Public class d:id{}
28:}
Then we add a configuration file for the application and define a configuration for unity as follows. This configuration defines a unity container called Defaultcontainer and completes the type matching of mappings between the interface defined above and the corresponding implementation class.
1: <configuration>
2: <configsections>
3: <section name= "Unity"
4: Type= "Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
5:microsoft.practices.un ity. Configuration "/>
6: </configsections>
7: <unity>
8: <containers>
9: <container name= "Defaultcontainer" "
: <register type=" Unitydemo.ia, Unitydemo "mapto=" UnityDe Mo. A, Unitydemo "/>
One: <register type=" Unitydemo.ib, Unitydemo "mapto=" unitydemo.b, Unitydemo "/>
1 2: <register type= "Unitydemo.ic, Unitydemo" mapto= "unitydemo.c, Unitydemo"/>
: <register t Ype= "Unitydemo.id, Unitydemo" mapto= "UNITYDEMO.D, Unitydemo"/>
: </container>
: </cont Ainers>
: </unity>
: </configuration>
Finally, a UnityContainer object representing the IOC container is created in the main method, and the configuration information is loaded to initialize it. It then invokes the resolve method of its generics to create an object that implements the generic interface ia. Finally, convert the returned object to type A and verify that its B, C, and D properties are empty.
1:static void Main (string[] args)
2: {
3: iunitycontainer container = new UnityContainer ();
4: unityconfigurationsection configuration = configurationmanager.getsection ( Unityconfigurationsection.sectionname)
5: As unityconfigurationsection;
6: configuration. Configure (Container, "Defaultcontainer");
7: A A = container. Resolve<ia> () as A;
8: if (null!= a)
9: {
Console.WriteLine ("A.B = null?") {0} ", a.b = = null? "Yes": "No");
One: Console.WriteLine ("A.C = null?") {0} ", A.C = = null? "Yes": "No");
Console.WriteLine ("A.D = null?") {0} ", A.D = = null? "Yes": "No");
: }
14:}
From the execution results given below we can conclude that the Resolve<ia> method returns an object of type A, with three properties of the object being initialized effectively. This simple program embodies interface injection (the corresponding implementation type is parsed by the corresponding interface), constructor injection (attribute B), property injection (attribute C), and method injection (attribute D). [Source code download from here]
1:A.B = null? No
2:A.C = null? No
3:A.D = null? No