Deconstruct control inversion (IOC) and dependency injection (DI) 1. Control Inversion
Inversion ofcontrol (IOC): in short, the Code controller is handed over to the system for control, rather than being inside the code. IOC eliminates direct dependencies between components or modules, this makes software system development more flexible and scalable. The typical application of control reversal is embodied in the design of the framework system and is the basic feature of the framework system. Both. NET Framework and Java framework are based on the idea of control reversal.
Control inversion is often seen as a synonym for the Dependency inversion principle. The concept is generally derived from the design of the framework system. For example,. NET Framework is a large framework system. On the. NET Framework Platform, you can easily build ASP. NET web applications, Silverlight applications, Windows Phone applications, or Windows azure cloud applications. In many cases, the method of building a custom System Based on. NET Framework is to expand the. NET Framework itself, call the basic API provided by the framework, and extend the custom system functions and behaviors. However, no matter how new or custom functions are created or extended, the final control of code execution is returned to the framework for execution, and then the application is returned. Mr. Huang once worked in objectbuilder
The Application Block article provides a more appropriate example, that is, in the window from application, after application. Run is called, the control of the program is transferred to the windowsfroms framework. Therefore, control inversion emphasizes the inversion of control, which reflects the Dependency inversion of the control process. In this sense, control inversion is a special case of dependency inversion.
2. Dependency Injection
Dependencyinjection (DI), which is early in Martin flower's Inversion of controlcontainers and the dependency injection pattern. Its definition can be summarized as follows:
The customer class depends on the abstract interface of the service class, and the specific service class instance is instantiated by other components (such as di container) according to the context environment at runtime, it is injected into the runtime environment of the customer class to achieve loose coupling between the customer class and the service class instance.
(1) three common injection methods
In short, the dependency injection method is summarized as the following three methods.
· Interface injection transfers the relationship between objects to an interface for interface injection control.
First, define the injection interface:
Public interface irunnerprovider
{
Void run (Action action );
}
The system in this example is a background processing program that provides a variety of runtime environments. By default, it runs on a separate thread, or run through an independent Windows service process, you need to implement different providers for different situations, such:
Public class defaultrunnerprovider: irunnerprovider
{
# Region irunnerprovider members
Public void run (action Action)
{
VaR thread = new thread () => action ());
Thread. Start ();
}
# Endregion
}
For the host class of the Background Service, the injected interface instance is obtained through configuration, and the execution process of the run method is injected with the logic defined by the interface, which is defined by the context Configuration:
Public class runnerhost: idisposable
{
Irunnerprovider provider = NULL;
Public runnerhost ()
{
// Getprovider by configuration
Provider = getprovider (config. Host. provider. Name );
}
Public void run ()
{
If (provider! = NULL)
{
Provider. Run () =>
{
// Exceute logic in thisprovider, if provider is defualtrunnerprovider,
// Then this logic will run in a new thread context.
});
}
}
}
Interface injection. It is possible to modify the injection logic without re-compilation. The getprovider method can completely read the config. host. provider. name content to dynamically create the corresponding provider, so as to dynamically change the backgroundhost run () behavior.
· Constructor injection. When a customer class is constructed, the service class instance is passed to the client as a constructor parameter. Therefore, Once injected, the service class instance cannot be modified.
Public class picworker
{
}
Public class picclient
{
Private picworker worker;
Public picclient (picworker worker)
{
// Inject through the constructor
This. worker = worker;
}
}
· Setter injection: sets server-class instances as client-class properties at runtime by setting client-class attributes. Compared with the constructor injection method, property injection provides the possibility of rewriting server-class instances.
Public class picclient
{
Private picworker worker;
// Inject data through attributes
Public picworker woker
{
Get {return this. Worker ;}
Set {This. worker = value ;}
}
}
In addition. under the net platform, in addition to the three injection methods proposed by Master Martin flower, there is also a more elegant choice, that is, relying on. net-specific attribute implementation, with ASP. for example:
[Httppost]
Public actionresult register (registermodel Model)
{
// The registration process is omitted.
Return view (model );
}
Httppostattribute injects the logic for Automatically Checking the POST request for registeraction by using attribute. The same injection method is widely used in many filter logics of ASP. net mvc.
[Attributeusage (attributetargets. method, allowmultiple = false, inherited = true)]
Public sealed class httppostattribute: actionmethodselectorattribute
{
// Fields
Private Static readonly acceptverbsattribute _ innerattribute = new acceptverbsattribute (httpverbs. post );
// Methods
Public override bool isvalidforrequest (controllercontext, methodinfo)
{
Return _ innerattribute. isvalidforrequest (controllercontext, methodinfo );
}
}
For details about attribute, see section 8.3 "historical entanglement: features and attributes". The trimattribute feature is a typical application of attribute injection for Attribute trim filtering.
This article is excerpted from the book ". Net (version 2nd) You must know ".
Book details: http://blog.csdn.net/broadview2006/article/details/6673353