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

Source: Internet
Author: User

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

The previous article mainly introduced IoC and its use in ASP. net mvc. This chapter describes how to use code to implement dependency injection in ASP. net mvc from the following points:
● AutoFac and Installation
● Container Creation
● Create a DependencyResolver Based on AutoFac
● Create ControllerFactory Based on AutoFac

AutoFac and Installation

Autofac is A. Net-based IoC container used to manage dependencies between classes. Autofac provides multiple methods to register a type to a container. It also provides multiple methods to inject dependencies into an instance, such as constructor injection, property injection, and method injection.
In this example, autofac is used as the dependency injection container to implement the dependency injection function in ASP. net mvc.
1. Use Nuget to install the Autofac component in the MyBlog project:

  

Container Creation

1. Preparations before creation:
Modify the BlogManager on which PostController depends to pass in through the constructor:

  

2. Create a New Static MyBlogContainer and static method GetContainer to create a container and put BlogManager and PostController into the container:

  

  

DependencyResolver Based on AutoFac

DependencyResolver in ASP. net mvc is not only used to create a Controller, but also can be used by the Controller to obtain components from the container. Create a custom DependencyResolver and replace the default one so that ASP. net mvc can obtain the BlogManager and PostController that were previously put into the container from the container.
1. Create a custom DependencyResolver:

  

Note: The exception is obtained because not all components are registered in the container. When the container obtains a component that does not exist, an exception is thrown. If no component is found, null is returned, the program calls other methods to instantiate components, such as Activator. createInstance method.
2. Call the create container and replace the parser in the Application_Start method:

  

3. Run the program. The manager is available in the breakpoint on the document list page, proving that the Controller has successfully called the parameter construction:

  

  

Create ControllerFactory Based on AutoFac

The previous article in ASP. net mvc Controller Factory describes how to create a Controller, while ASP. net mvc has a default Controller Factory type DefaultControllerFactory. It has two main steps to create a Controller: obtaining the Controller type (with a cache mechanism) and creating a Controller (using IControllerActivator ).
To obtain ControllerFactory from the IoC container when creating the Controller, you only need to replace it with the creation method. That is to say, either replace IControllerActivator or directly skip IControllerActivator and directly create from the container, because the two methods are similar, this example only skips the IControllerActivator version:
1. inherit defacontrocontrollerfactory to customize the new ControllerFactory (because you also need to use the Controller type to obtain the link, it is most convenient to directly inherit), reload the protected GetControllerInstance, in addition, if the corresponding Controller cannot be found in the container, you can continue to call the basic class creation method to ensure that other controllers that are not registered with the container can also be created correctly:

  

2. Replace the Controller factory in the Application_Start method:

  

3. Run the program. The program can also work normally:

  

Note: Because PostController is deleted from the non-argument constructor and the manager instance must be assigned a value in the constructor, an error occurs if you do not use a container to create the Controller:

  

  

Summary

This article uses AutoFac to implement Dependency Injection Based on Dependency Resolver and ControllerFactory in ASP. net mvc, and verifies the analysis in the previous article in the form of code. But it should be noted that the above Code is only the Demo for the experiment, Autofac is a relatively complex IoC container, to use it well can refer to the official documentation http://autofac.readthedocs.io/en/latest/getting-started/index.html or wait for subsequent articles to introduce.
There are no good or bad extension methods for ASP. net mvc dependency injection. The choice of extension methods is more dependent on the actual situation.

In addition, although dependency injection is implemented in this article, when the Dependency inversion principle DIP is not actually used, in other words, PostController still depends on the implementation of BlogManager, which is unreasonable, the code for MyBlog will introduce how to refactor in subsequent articles.

Refer:
Http://autofac.readthedocs.io/en/latest/getting-started/index.html

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

ASP. NET has no magic-directory

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.