ASP. net mvc controller (1)
Preface
In the routing chapter, I explained the role of the routing, and talked about the controller section. From this article, I will explain how the Controller in MVC came from? What does the MVC Framework do to it? And the questions left in the previous sections will be resolved in this section.
Summary of controller Activation
In general, the Controller activation process involves the following steps (in part ):
1. Obtain according to current route informationController name
2. Get the Controller factory of the current system (used to generate the Controller)
2.1 dataController nameGenerated and the current system'sRequest context parametersGenerateController type (Type)
2.1.1 determine the namespace of the controller based on the current route information
2.1.2 returnController type(Type)
2.2 AccordingController type(Type)AndRequest context parametersGenerateController type(IController)
2.3 ReturnController type(IController)
3. ObtainController(IController)
4. Execute IController. Execute ()
Origin of Controller
As mentioned above, the entry of MVC is in the Module. Specifically, when registering a route, MvcHandler is registered by default as the request processing type, and the Controller is produced here, why production? Because the system has implemented a controller factory class DefaultControllerFactory (the following code structure) in advance, many types and controller object models are involved in the process from controller generation to execution, these contents will be explained in the following sections.
DefaultControllerFactory type structure:
1 public class DefaultControllerFactory : IControllerFactory 2 { 3 public DefaultControllerFactory(); 4 public DefaultControllerFactory(IControllerActivator controllerActivator); 5 6 public virtual IController CreateController(RequestContext requestContext, string controllerName); 7 protected internal virtual IController GetControllerInstance(RequestContext requestContext, Type controllerType); 8 protected internal virtual SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, Type controllerType); 9 protected internal virtual Type GetControllerType(RequestContext requestContext, string controllerName);10 public virtual void ReleaseController(IController controller);11 }
This article will give a rough explanation of the previous section 2.1.2 in the above summary. Please take a look at it first:
For example, the first thing to say here is the Controller type cache object ControllerTypeCache. ControllerTypeCache loads all the public classes that implement the IController interface and caches them in the MVC-ControllerTypeCache.xml file. Of course, these are all done by the framework. We only need to understand and learn the ideas.
When the request arrives at the default request handler, the default controller factory DefaultControllerFactory determines Based on the namespace defined in RouteData DataToken [NameSpaces] and the controller name of Values [controller, the ControllerTypeCache object is used to query matching information.
First, query whether the Controller corresponding to this name exists in the cache based on the Controller name. If yes, it is stored in the ILookup <string, Type> Type object, then, compare the namespace of the controller Type in the ILookup <string, Type> object with the namespace defined in the rouoken [NameSpaces] of RouteData, if they are the same, add this type to the returned set. If they are not the same, continue to use the namespace values of the RouteData DataToken [NameSpaces] to compare them one by one.
If the total number is 0 and the total number is 1, the type in this set is returned. If the total number is greater than 1, an exception of the CreateAmbiguousControllerException type is thrown.
At this time, the ultcontrollerfactory has obtained the Controller Type ).
In summary, section 2.2 refers to the implementation of the IControllerActivator interface type.
1 // Summary: 2 // fine-grained control over how dependencies are injected into the instantiated controller. 3 public interface IControllerActivator 4 {5 // Summary: 6 // The controller is created in the class. 7 // 8 // parameter: 9 // requestContext: 10 // request context. 11 // 12 // controllerType: 13 // controller type. 14 // 15 // return result: 16 // The created controller. 17 IController Create (RequestContext requestContext, Type controllerType );
The implementation of this part can be injected into the Controller factory, while the implementation of the internal still has a scalable injection place, there is a default implementation in the MVC framework, let's take a look at the implementation concept diagram after Part 1:
After obtaining the Type of the Controller, DefaultControllerFactory can create a Controller based on the Type. However, in the design of the MVC framework, [Create a Controller based on ControllerType] will not be placed in defacontrocontrollerfactory, instead, the default implementation class DefaultControllerActivator of the IControllerActivator interface type is implemented in the MVC framework to create the IController, in DefaultControllerActivator, The DependencyResolver type is used to create a default implementation class of the IDependencyResolver interface.
In the IDependencyResolver interface, there is a GetService () method. This method is used to create a type and can be customized. This is also an extension point. Description: interface type. The default implementation method of the IDependencyResolver interface is ultdependdependencyresolver in MVC. The default implementation method of the GetService () method in the defadependdependencyresolver type is Activator. createInstance (serviceType); that is, normal reflection is used to create a type.
Let's take a look at a process diagram from Handler to Icontroller:
The above and above sections are all implemented by MVC by default. Each part can be customized and extended, such as MvcHandler, DefaultControllerFactory, and DefaultDependencyResolver.
The following sections describe all the extensions that can be injected when the controller is activated.
Author: Jin Yuan
Source: http://www.cnblogs.com/jin-yuan/
The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.