An advanced path to ASP. NET MVC: In-depth understanding of dependency injection (DI) and inversion of control (IOC)

Source: Internet
Author: User

0x1 What is Dependency injection

Dependency Injection (Dependency injection) is a process in which a customer class relies on only one interface of the service class and does not rely on a specific service class, so the customer class defines only one injection point. In the process of running the program, the client class does not instantiate the specific service class instance, but the client class's running context or specialized component is responsible for instantiating the service class and injecting it into the customer class to ensure the normal operation of the customer class. A detailed introduction can be read in the previous post: http://www.cnblogs.com/dazhuangtage/p/5672190.html

0X2 What is control reversal

Before explaining what a control reversal is, let's introduce the previous blog post. The question raised by a blogger in the comments: is dependency injection and control inversion not the same thing?

In my opinion, the ultimate realization of the idea of inversion of control is to rely on the implementation of this approach with dependency injection. Control inversion is only to change the relationship between the high and low, the previous level of the module in the implementation of the function may be dependent on the upper module, through control inversion can let the underlying module depends on an interface, if this time the high-level module to use the underlying module must be

Implement this interface and then register the implementation class of the high-level module with the underlying module by means of dependency injection.

It is possible that the above explanations are quite confusing, and I will introduce the design pattern of control inversion by example.

0X3 An example of understanding control inversion

  

It can be seen that drivers rely on the "car" and "train" these two classes, if the driver needs to drive a car, you need to instantiate a car class, need to open a train, you need to instantiate a train class

  

Drive car cat=new car (); Cat. Stop ();//Open train omitted.

What if we're going to have to fly a plane this time? The traditional approach is to create a new aircraft class and then instantiate the aircraft class directly from the driver.

We have the framework of the "Autonomous driving system", now "autopilot system" already has the function of auto-driving, flying, train, you feel very strong, so the system sold to a company, but the company's business is not limited to the first three modes of transport, Now the company is going to realize that the driver can drive the ship, as shown in

As you can see, if our autopilot system is going to be able to drive a spaceship, we need the driver to create the "spaceship" object, and our framework depends on the outside (because the ship class is on the client side). This is what we often call the underlying module dependent on the Advanced module. This reliance is certainly not going to work, and as customers change our framework, it certainly doesn't, and we continue to evolve.

Now our driver is not directly dependent on a specific implementation class, and depends on the interface, but this time exposed a problem, I hope you do not look at the following text, we can first think about what the problem. (three transport and interface relationships are identified incorrectly, should be implemented)

Design solves the problem of not using the new specific object, but the new problem comes with it: which implementation class does the driver use? We all know that interface is not directly instantiated, can instantiate only the specific implementation class of the interface. OK, in order to solve this problem we continue to evolve

We use the factory model, when the relationship is the driver relies on the factory class, specifically to create the specific implementation class. According to our view, if we implement the spacecraft, then what will happen to the relationship between them (three vehicles and interfaces are identified incorrectly, should be implemented)

Through a series of evolution, now the relationship between the two has been completely changed, the former is the bottom module (framework) relies on the high-level module, now become the high-level module depends on the underlying module, as can be seen, whether you want to open a spaceship or open a rocket, as long as you realize the "Transport" interface, Then I can create it in the factory class. This not only increases the scalability of our system, but also improves the overall robustness of our system.

Finally, to summarize what is control reversal, my answer has been given at the beginning of the article, the following is the answer to Wikipedia:

    

Control inversion (inversion of controls, abbreviated to IOC) is a design principle in object-oriented programming that can be used to reduce the degree of coupling between computer code. The most common of these is called Dependency injection (Dependency injection, or DI), and there is a way to call it "dependency lookup" (Dependency lookup). By controlling the inversion, the object is created by an external entity that regulates all objects within the system, passing a reference to the object on which it depends. It can also be said that the dependency is injected into the object.

Late Night code word, after reading feel useful to give a recommendation bar ..... Welcome to spray me, spray I can communicate, communication can progress.

An advanced path to ASP. NET MVC: In-depth understanding of dependency injection (DI) and inversion of control (IOC)

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.