Analysis Ioc (Inversion of Control) Controller of how to achieve Autofac

Autofac Ioc is a framework, the biggest feature should be able to do the configuration file directly using C # code to register.

Autofac also provides for the expansion of Asp.net MVC.

Here is Autofac document describes how to integrate using MVC project:

Copy the code
protected void Application_Start()
{
    var builder = new ContainerBuilder();
    builder.RegisterControllers(typeof(MvcApplication).Assembly);
    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    // Other MVC setup...
Copy the code

 

Inside explain the process:

1. First create a ContainerBuilder (which will later provide a container for us, which we can remove the object instance we need)

2. Register All Controllers in the current Assembly Builder, Builder so you get all types of current Controller MVC project

3. Create a container

4. Replace the default MVC DependencyResolver with AutofacDependencyResolver

 

OK. Here, in front of the still easy to understand, the last DependencyResolver do what? To understand DependencyResolver, you need to know ControllerFactory

MVC in ControllerFactory

MVC process request, the URL request is to find matching Route, Route parsed names corresponding to the Controller,  and then by name, to find the corresponding type Controller, and a Controller object instance in response to the request.

 

Bolded above, is DefaultControllerFactory do, that is, its methods CreateController method:

public virtual IController CreateController(RequestContext requestContext, string controllerName)

 

CreateController method and mainly relies on GetControllerType and GetControllerInstance method:

protected internal virtual Type GetControllerType(RequestContext requestContext, string controllerName)

protected internal virtual IController GetControllerInstance(RequestContext requestContext, Type controllerType)

 

See so many virtual method is not very exciting? This is not clear is we inherit and override thing. If we override GetControllerInstance method here, and then get an instance of this ControllerType from Ioc container according to controllerType here, everything is OK not to do?

 

Yes, inheritance and rewrite GetControllerInstance method can indeed achieve Ioc Controller, and then in Application_Start () inside, use this line of code, replace DefaultControllerFactory.

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory(_container));

 

DependencyResolver it that good?

In fact DefaultControllerFactory in GetControllerInstance, calls the method IDependencyResolver interface definition GetService Gets an instance. By inheriting this interface, replace the original DependencyResolver, changes to the original MVC smaller. So to better use by inheritance IDependencyResolver interface that's opening we see:

 
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

IDependencyResolver 借口
Copy the code
public interface IDependencyResolver {

       object GetService(Type serviceType);

       IEnumerableGetServices(Type serviceType);

}
Copy the code

 

AutofacDependencyResolver want to see the source code for an example of how the Type acquired from the container can to here

 

Thinking and practice: When used ControllerFactory

Area of ​​use Asp.net MVC in, should we are more familiar. It is used to solve large projects, the development of the situation people. Principle area to achieve, is to distinguish even by the same name Controller namespace. But in any case, area is the inside of a project.

 

How can separate area to a different project in it? Here's GetControllerType method is a good starting point. Next explore how the area separated into different project, the convenience of a larger project. This is also the principle Orchard CMS module to achieve.

Guess you like

Origin www.cnblogs.com/Freedom0221/p/12499377.html