Dependency Injection, this professional word we can be divided into two parts to understand:
Dependency, which is the dependency of the relationship between things in UML, describes the use of things a in some cases B, the change of things B will affect things A;
Injected, the doctor injects the drug into the patient's body through a needle. Injection is to inject and instill something in the outward.
In general, dependency injection is a Class A dependent class B, and an instance of Class B is injected externally to a and not instantiated or initialized by a itself.
three methods of injection
one. constructor injection
Class A relies on Class B, in the constructor of Class A, with a parameter of Class B, which is passed from the outside for Class B when the new Class A is a construct injection
Class program{ static void Main (string[] args) { var b = new B (); var a = new A (b);} } Class a{ private B _b; Public A (b b) { this._b = b; }} Class B {}
Constructor injection is the default behavior and does not need to be set, AUTOFAC automatically completes the work of construction injection.
Two. Attribute Injection
Modify the Class A above, expose the variable _b through the properties and delete the constructor method, and then see how we implement the attribute injection when we write the code normally:
Class program{ static void Main (string[] args) { var a = new A (); var B = new B (); A.B = B; To inject a dependency B} through a property
Let's take a look at how AUTOFAC is injected into the property:
All injection methods for attribute injection are defined at the time of registration.
Class program{ static void Main (string[] args) { var builder = new Containerbuilder (); By propertiesautowired, type A is automatically injected with a property builder when it is acquired . Registertype<a> (). Propertiesautowired (); Builder. Registertype<b> (); var container = Builder. Build (); var a = container. Resolve<a> (); Console.Write ("Press any key to continue ..."); Console.readkey (); }}
Using propertiesautowired can also only specify that a class will automatically inject property. The Propertiesautowired method automatically injects all the properties that can be injected, but if you want to inject only a few properties, you can use several injections except propertiesautowired, Withproperty is one of them:
Class program{ static void Main (string[] args) { var builder = new Containerbuilder (); Builder. Registertype<a> (). Withproperty (New Namedpropertyparameter ("B", new B ())); Builder. Registertype<a> (). Withproperty ("B", new B ()); The effect is the same as above
var container = Builder. Build (); var a = container. Resolve<a> (); Console.Write ("Press any key to continue ..."); Console.readkey (); }}
In the register, there is a kind of lambda registration, lambda registration, because it is a write lambda expression to register, its lambda content can be written a lot, where you can do property injection:
var builder = new Containerbuilder (); builder. Register (c =>{ var _a = new A (); _A.B = new B (); Manual injection of return _a;});
Three-way injection
There are two ways of method injection, the latter two of which are attribute injection: Lambda and event
var builder = new Containerbuilder ();//Lambdabuilder. Register (cc =>{ var _a = new A (); _a.methodinjection (New B ()); return _a;}); /Event Builder. Registertype<a> (). OnActivated (e =>{ e.instance.methodinjection (New B ());});
Methodinjection is a method of a and it requires a parameter of type B, which we pass in by means of an external method, which is the method injection
The End Statement
It is also recommended to use the default simplest construction injection, attribute injection is recommended to set automatic attribute injection, and method injection is not recommended. In fact, the recommended principle here is that it is not very good to specify the injection method at the time of registration, because later people may not be aware of each type of injection rules, but also need to go to the registered place to view, and different people write different, so easy to confuse. It is also not very appropriate to inject at the time of acquisition, because in the actual usage we associate the registration type with the interface and get the interface type directly in the fetch. And because we get the interface type when we get it, we can't guarantee that the actual implementation of the interface has our expected parameters.
IOC container AUTOFAC-Dependency Injection (vi)