1. Control reversal
Inversion of control (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, makingSoftwareSystem development is 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 gave a more appropriate example in the object Builder application block article, that is, in the window from application program, when the application. after running is called, control of the program is transferred to Windows froms 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
Dependency injection (DI), which is early in Martin flower's Inversion of control containers 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 ()
{
// Get provider by configuration
Provider = getprovider (config. Host. provider. Name );
}
Public void run ()
{
If (provider! = NULL)
{
Provider. Run () =>
{
// Exceute logic in this provider, 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 InjectionServerWhen a class instance is running, it is set to a customer class attribute. Compared with the constructor injection method, property injection provides the possibility of rewriting the server class instance.
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. the action filter in. Net MVC is used as an example:
[Httppost]
Public actionresult register (registermodel Model)
{
// The registration process is omitted.
Return view (model );
}
Among them, httppostattriister injects the logic of automatic check POST request for register action through attribute mode. 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.
From: http://www.cnblogs.com/jyshis/archive/2011/09/15/2177279.html