ASP. NET has no magic -- ASP. net mvc IoC, mvcioc

Source: Internet
Author: User
Tags mysql injection

ASP. NET has no magic -- ASP. net mvc IoC, mvcioc

The previous article introduced how MVC creates a Controller through ControllerFactory and ControllerActivator, and how the Controller completes function expansion and business execution through the template ControllerBase. The design of this series of MVC types reflects the IoC design principles everywhere. Therefore, this chapter will introduce the IoC in ASP. net mvc from the following points:
● What is IoC?
● IoC in ASP. NET MVC
● What is DI
● IoC container and Dependency Parser (Dependency Resolver)
● Use DI in ASP. NET MVC

What is IoC?

IoC (Control Inversion, Inversion of Control) is a design principle in software development, meaning that Control is taken from itself (here most of itself refers to business code) transfer to other objects for control. The objective of IoC is to decouple the execution of tasks from the implementation, and focus on the design of module tasks (for example, the components that operate databases can operate SQL Server and My SQL ), the system itself does not pay attention to what this module has done, and will not affect the system when the system replaces this module. There are several common implementation methods based on IoC in software development:
● Factory Model
● Template Mode
● Policy Mode
● Dependency Injection

All of the above models are decoupled to facilitate expansion. In particular, Dependency Injection (DI, Dependency Injection). If you see IoC, you can think of Dependency Injection more often.
Dependency inversion principle (DIP): There are so many implementations mentioned above, but one fundamental principle is the Dependency inversion principle."High-level modules do not depend on low-level modules. They should all depend on abstraction. abstraction should not depend on implementation. Implementation should depend on abstraction". In. net is to use interfaces and abstract classes to abstract the business and objects. abstract and implementation are separated. If this principle is abandoned, implementation depends on implementation, then, the reverse will not be able to happen (lll contains ω limit ).

IoC in ASP. NET MVC

In the previous article, we analyzed the process of creating a Controller in ASP. net mvc and the main objects involved in the execution:
● DefaultControllerFactory: factory mode.
● DefaultControllerActivator: dependency injection (Note: when creating a Controller, you must first obtain the Controller instance from the dependency parser. You cannot obtain the instance before creating it yourself ).
● ControllerBase: Template mode (Note: ControllerBase defines the Execute template method to call the abstract method ExecuteCore, and the implementation of ExecuteCore is in the subclass ).

The design idea of these objects is IoC.

What is DI

DI (Dependency Injection, Dependency Injection) is actually two parts, the first is "Dependency", and then "Injection ".
1. Dependency: In simple terms, it is needed. In an object-oriented class, it "Needs" other types to complete the work. For example, the logic class in MyBlog requires a storage class to operate the database. In this case, the logic class depends on the storage class:

2. injection: in fact, it is to assign values to class dependencies. Common injection methods include constructor injection, attribute injection, and method injection. To put it bluntly, it is through the constructor method and attribute (Setter method) and the method to pass the dependent object into the instance of the class and assign a value. However, it should be noted that the word "NOTE" does not directly use a value assignment?
For example, make a water bulb, either leave a water injection, or fill the water into it during preparation and block it.
For water balls with water injection ports, if red water is injected during use, it turns into a red water ball, And the injected yellow water is yellow, the blocked water balls are filled with the color of water.
This is especially true in program development. Taking MyBlog as an example, the business logic class obtains data through the storage class, and the business class can be "sealed" and can be opened only by the SQL Server operation class, when the instance is "injected", the SQL Server operation class will use SQL Server, and MySQL injection will use My SQL.
Water Injection requires water injection, and water injection is required. What are the "water" and "Water Injection" in the program?

IoC container and dependency Parser (DependencyResolver)

The so-called "water" in a program should be those abstract implementations, or abstract types. For example, the SQL Server warehouse operation class and the MySQL warehouse operation class. So where should these "water" be stored? The "water" must be placed in the container, so there is an IoC container.
What is the IoC container? It can be as simple as an array or dictionary of an instance. The corresponding instance can be obtained from the dictionary or array through the instance type (or implemented parent type, interface, etc, it can also be sophisticated IoC container components such as Autofac and Ninject.
The following code is the process of injecting "water" to containers in the AutoFac and NinJect official documents:
Ninject: https://github.com/ninject/ninject

  

AutoFac: https://autofac.org/

  

With containers filled with "water", if you want to inject "water" into the program, you need the support of the "water injection" dependency Parser (Denpendency Resolver.
The following code creates a Controller using DefaultControllerActivator:

  

Imagine connecting the above "Water Injection" to a container filled with "water". Isn't it possible to inject what is needed into the desired position?

Use DI in ASP. NET MVC

DI depends on the container and dependency parser, ASP. NET is recommended to use mature dependent containers, such as Autofac. They provide powerful functions and support multiple components to register with containers and injection methods. The dependency parser has a default parser in ASP. net mvc. The following is the parser definition of ASP. net mvc:

  

It actually gets an IDependencyResolver object through the Current attribute.
DependencyResolver has built-in objects that implement this interface, but it is almost useless, such:

  

Therefore, if the IDependencyResolver interface is implemented in ASP. NET, the default dependency parser is modified using the DependencyResolver. SetResolver method.

In ASP. in. net mvc, Controller is the most frequently used dependency injection. Controller is the entry to business logic execution. It depends on business logic components, and business logic components depend on data operation components. Therefore, using dependency injection in ASP. net mvc is actually to put the Controller and its dependencies into the container, and then obtain it from the container when creating the Controller. You can use the following methods to create a Controller:
1. Implement the IDependencyResolver interface and then use the DependencyResolver. SetResolver method to modify the default dependency Parser (replace the default "injection ").
2. Implement the IControllerActivator interface, and then pass it into DefaultControllerFactory as a parameter when creating defacontrocontrollerfactory (add your own "Water Injection" in ControllerActivator and replace the original ControllerActivator ).
3. inherit defacontrocontrollerfactory and replace the original GetControllerInstance method with the method of getting Controller from the container (Add "Water Injection" to ControllerFactory and discard the use of ControllerActivator ).

Note: Because the Controller type contains an IDependencyResolver attribute, the default dependency parser is not replaced by method 2 and method 3, which will make the Controller unable to access the real container by using this attribute, if you need to replace the default dependency parser with method 1.

Summary

IoC containers and dependency injection are a very important concept in software development. Currently, the core of some mainstream development frameworks is based on dependency injection, that is, all components in the framework, such as logs, caches, and queues, will be injected to the used place through the container. This chapter mainly introduces the concept of IoC and DI and Its Application in ASP. net mvc, the next article will introduce how to use these three methods in the form of code.. net mvc.

Refer:

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

Connection: http://www.cnblogs.com/selimsong/p/7682808.html

ASP. NET has no magic-directory

ASP. NET has no magic-ASP. net mvc IoC code

Related Article

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.