What are dip, IOC, and Di?

Source: Internet
Author: User
Document directory
  • Three Common injection methods
What are dip, IOC, and Di?

 

Dip

Dependency inversion principle Dip (dependency-inversion Principles)

 

IOC

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, 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 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.

 

Di

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.

Three Common injection methods

In short, the dependency injection method is summarized as the following three methods.

Interface Injection(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
{
public void Run(Action action)
{
var thread = new Thread(() => action());
thread.Start();
}
}

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 InjectionConstructor injection. When a customer class is constructed, the service class instance is passed to the client as a constructor parameter. Therefore, once the service class instance is injected, it cannot be modified.

Public class picworker
{
}

Public class picclient
{
Private picworker worker;

Public picclient (picworker worker)
{
// Inject through the constructor
This. worker = worker;
}
}

 

Property Injection(Setter injection), set the server class instance to the client class attribute at runtime by setting the client class attribute, 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 ;}
}
}

 

Link

In general, dip, IOC, and Di have a constantly broken relationship. Dip is a theoretical summary of dependency, while IOC and Di are embodied in a specific practice model. IOC and di provide effective solutions to eliminate the coupling between modules or classes, thus ensuring the dependency on abstract and stable modules or types, which means that they adhere to the general direction of the dip principle.

However, the differences between IOC and Di are mainly reflected in the differences in scenarios of interest: IOC emphasizes the reversal of control and focuses on process control; di focuses on the "inversion" between layers and layers, components and components, modules and modules, or between types and types, which is embodied in the dependency mode deconstruction on the design model.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.