asp.net mvc controller activation full analysis

The activation of the controller is implemented by default using reflection, which adopts design patterns such as DI and singleton. For the controller, the following classes are mainly involved:
ControllerBuilder, DefaultControllerFactory, DefaultControllerActivator (implementing the IControllerActivator interface), DependencyResolver (not implementing IDependencyResolver, but having an instance of this interface object), DefaultDependencyResolver (implementing the IDependencyResolver interface, and this class is used by default. resolve controller object)

The above classes are the most important classes for controller activation. I can roughly talk about the process and memos, of course, I can’t say too much. It’s good to have a general idea. There are already many articles on the Internet. The DefaultControllerFactory is not the place to construct the controller. The controller is constructed in the ControllerBuilder, which specifically involves the SetControllerFactory and GetControllerFactory methods. We can expand our own IOC container through SetControllerFactory, which is the first extension point.
The DefaultControllerActivator class is used to activate the controller. When we generally create our own MVC framework, we use reflection to create a controller instance through the obtained controller object in the Create method of the ICOntrollerActivator interface implemented by this class. Of course, this is when we create our own MVC framework and do not use third-party IOC containers, we use this method to create. This is also the second extension, we can create controller objects by implementing the IControllerActivator interface in a third-party IOC container.
The DefaultDependencyResolver class implements the IDependencyResolver interface, a container designed by the IOC container (based on the DI pattern) implemented by the default MVC framework. We constructed our IOC container here, but by reading the source code, you will find that the source code of the IOC container actually uses reflection to create controller objects by default. This is the same as the DefaultControllerActivator class above. You can refer to the source code:

public IController Create(RequestContext requestContext, Type controllerType)
            {
                try
                {
                    return (IController)(_resolverThunk().GetService(controllerType) ?? Activator.CreateInstance(controllerType));
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException(
                        String.Format(
                            CultureInfo.CurrentCulture,
                            MvcResources.DefaultControllerFactory_ErrorCreatingController,
                            controllerType),
                        ex);
                }
            }

You can see the code in the try statement above. By default, MVC's own IOC container (ie the DefaultDependencyResolver class) is used. Here we can expand our own design logic based on third-party IOC, which is also the third expansion point.

The book <<asp.net mvc5 framework reveals the secrets>> describes in detail the three extensible places as described above. The above description is a summary of my own reading of the source code and information. When you look at the MVC source code, you will have your own gains, and I have not spread them all out, just my own notes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324522490&siteId=291194637