Asp. NET no magic--asp.net MVC IoC

Source: Internet
Author: User

The previous article describes how MVC creates a controller through Controllerfactory and Controlleractivator, And how the controller through Controllerbase this template to complete the expansion of the function and business execution. This series of MVC types is designed to reflect the IOC's design principles, so this chapter will cover the IOC in asp:
What is IOC
IOC in ASP.
What is di
IOC container with Dependent parser (Dependency Resolver)
Using Di in ASP.

What is IOC

IoC (controlled inversion, inversion of control) it is a design principle in software development, which means that control is transferred to other objects from itself (the majority of itself refers to the business code). The IOC's intention is to decouple the implementation of the task from implementation , and to focus on the design of the module task (e.g., the components of the operational database, the operations of SQL Server and my SQL), and the system itself does not care what this module does, while the system The system is not affected by the replacement of this module . There are several common implementations of IOC-based thinking in software development:
Factory mode
Template mode
Policy mode
Dependency Injection

All of the above modes are decoupled for ease of expansion. In particular, dependency injection (di,dependency injection), see the IOC think more about the dependency injection.
Dependency inversion principle (DIP): It says so many implementations, but one fundamental principle is that the dependency inversion principle "high-level modules do not depend on the lower layer modules, they should all depend on the abstraction, the abstraction should not depend on the implementation, the implementation should be dependenton the abstraction." In. NET is the use of interfaces and abstract classes to abstract the business and objects, abstraction and implementation separate, if the abandonment of this principle implementation relies on the implementation, then can not be reversed (Lll¬ω¬).

IOC in ASP.

In the previous article, we analyzed the creation process of controller in ASP and the main participating objects in the execution:
Defaultcontrollerfactory: Factory mode.
Defaultcontrolleractivator: Dependency Injection (note: When a controller is created, the controller instance is first obtained from the dependency resolver and cannot be acquired for its own creation).
Controllerbase: Template mode (note: Controllerbase defines the implementation of the Execute Template method call abstract method Executecore,executecore in the subclass).

The idea of these objects is the IOC.

What is di

DI (Dependency injection, Dependency injection), it is actually two parts, the first one is "dependent", then "inject".
1. Dependency: Simply, it needs to be done in an object-oriented class that "requires" other types to do the work. If the logic class in MyBlog needs a warehousing class to manipulate the database, in this case the logical class relies on the warehousing class:

2. Injection: In fact, it is the dependency assignment of the class, the commonly used method of injection is: construct injection, attribute injection, method injection, in the white is through the construction method, property (setter method) and method to pass the object of the dependency into the instance of the class and assign the value. But pay attention to this "note" word, why not directly with the assignment?
For a chestnut: make a water polo, or leave a spout, or pour it into a seal when it is made.
For the water polo with the injection port, if the red water is injected into the use, it becomes the red ball, and the yellow one is yellow, and the water polo that is sealed is the color of what color is filled in when it is made.
In the development of the program is more so, take MyBlog as an example, the business logic class through the storage class to obtain data, the business class can be "sealed" only with SQL Server operation class, can also open, at run time the data operation this instance "inject", then injected SQL Server operation class using SQL Server, use my SQL to inject MySQL.
Water polo first of all have to be flooded, and then the water polo to have a water injection port, then in the program of the "Waters" and "injection port" is what?

IOC Container and Dependency resolver (dependencyresolver)

The so-called "water" in the program should be those abstract implementations, or the types of abstractions that are implemented. such as the operation of SQL Server storage class and operation of MySQL warehousing operations class. So where do these "water" places go? "Water" is, of course, placed in a container, so there is the IOC container.
What is the IOC container? It can be as simple as an array or dictionary of just one instance, get the corresponding instance from this dictionary or array through the type of instance (or the implemented parent type, interface, etc.), or it can be complex and mature IOC container components like AUTOFAC and Ninject.
The following code is the process of injecting "water" into the container in the official documents of AUTOFAC and Ninject, respectively:
Ninject:https://github.com/ninject/ninject

  

autofac:https://autofac.org/

  

With a container full of "water", to inject "water" into the program, it is necessary to support the "injection port"-dependent parser (Denpendency Resolver).
The following code is the code that Defaultcontrolleractivator creates the controller:

  

Imagine connecting the "water injection port" above to a container filled with "waters", not being able to inject what is needed into the desired position?

Using Di in ASP.

The use of Di has two prerequisites to rely on containers and dependent parsers , ASP. NET suggests using mature, dependent containers such as AUTOFAC, which provide powerful functionality and support for registering multiple components into containers and injection methods. With respect to the dependency parser in ASP. NET MVC, there is a default parser, and the following is the parser definition for asp:

  

Its actual function is to get a Idependencyresolver object through the current property.
Some objects that implement this interface are built into the dependencyresolver, but are almost useless, such as:

  

So in general, if the Idependencyresolver interface is implemented in ASP. Then the default dependent parser is modified by the Dependencyresolver.setresolver method .

In ASP., the most frequent use of dependency injection is controller,controller as the gateway to the business logic, which relies on the components of the business logic, and the business logic components depend on the data manipulation components. So using dependency injection in ASP. is essentially putting the controller and its dependencies in the container, and then creating the controller to get it from the container. The use method is divided into the following according to the controller's creation process:
1. Implement the Idependencyresolver interface and then modify the default dependency resolver by using the Dependencyresolver.setresolver method ( replace the default "injection port").
2. Implement the Icontrolleractivator interface and then, when creating the defaultcontrollerfactory, pass it as a parameter into the defaultcontrollerfactory ( add your own "water injection port"in the Controlleractivator, and replace the original controlleractivator).
3. Inherit defaultcontrollerfactory Replace the original GetControllerInstance method with the method of acquiring the controller from the container (add "injection port" Place in Controllerfactory , discard the use of controlleractivator).

Note: Because a Idependencyresolver property is included in the controller type, Method 2 and Method 3 do not replace the default dependency resolver, which causes the controller to use the property to not access the real container. If you need to use Method 1 on its basis, replace the default dependent parser.

Summary

IOC containers and dependency injection are a very important concept in software development, and now the core of some of the mainstream development frameworks is based on dependency injection, where all components in the framework, such as logs, caches, queues, and so on, are injected into use by containers. This chapter introduces the concept of IOC, DI, and the three ways in which it is used in ASP. In the next article, we'll show you how to implement dependency injection in ASP. NET MVC using these three methods.

Reference:

Https://en.wikipedia.org/wiki/Inversion_of_control

This article connects: http://www.cnblogs.com/selimsong/p/7682808.html

Asp. NET no magic--Directory

Asp. NET no magic--asp.net MVC IOC Code Chapter

Asp. NET no magic--asp.net MVC 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.