ASP. NET MVC Controller activation (iii)

Source: Internet
Author: User

ASP. NET MVC Controller activation (iii)

Objective

In the last section of the GetControllerInstance () method from the controller factory to perform the injection of the controller, this article is about some of the object types involved in the default implementation of the GetControllerInstance () method, using them for controller injection.

Object model

Figure 1

As in 1, as described in the controller (a), the entire execution process is included in the GetControllerInstance () method in the Defaultcontrollerfactory controller factory, Let's take a look at the definition of the Icontrolleractivator interface type:

1      Public InterfaceIcontrolleractivator2     {3         //Summary:4         //when implemented in a class, creates a controller. 5         //6         //Parameters:7         //RequestContext:8         //The request context. 9         //Ten         //Controllertype: One         //The controller type.  A         // -         //return Result: -         //the controller that was created.  the IController Create (RequestContext requestcontext, Type controllertype); -}

You will find that the signature of the Create () method in the definition inside is the same as the GetControllerInstance () method signature in the Defaultcontrollerfactory controller factory, which is right, This is another injection point in the GetControllerInstance () method, and in the default execution of the framework, There will be a default implementation class that implements the Defaultcontrolleractivator type of the Icontrolleractivator interface type, because it is inside the framework and the access level is private, so we can't call outside, We can customize the implementation here and inject it into the framework.

Injection of the Icontrolleractivator type implementation

Let's take a look at our custom implementations:

1 usingSYSTEM.WEB.MVC;2 usingNinject;3 usingConsoleApplication2;4 5 namespaceMvcapplication.customcontrolleractivator6 {7      Public classNinjectcontrolleractivator:icontrolleractivator8     {9 Ten         PrivateIkernel _ninjectkernel; One  A          PublicNinjectcontrolleractivator () -         { -_ninjectkernel =NewStandardkernel (); the_ninjectkernel.bind<idatastandard> (). To<dataprovide>(); -         } -  -          PublicIController Create (System.Web.Routing.RequestContext requestcontext, Type controllertype) +         { -IController controller = _ninjectkernel.get (controllertype) asIController; +             if(Controller! =NULL) A             { at                 returnController; -             } -             return NULL; -         } -}

The implementation is almost identical to the one in the previous article, and now it is only possible to register its instance in the framework when the framework is initialized. Or global.asax in the Application_Start () method to register:

1 defaultcontrollerfactory defaultcontrollerfactory =2                 new defaultcontrollerfactory (new  customcontrolleractivator.ninjectcontrolleractivator ()); 3 ControllerBuilder.Current.SetControllerFactory (defaultcontrollerfactory);

Here is the defaultcontrollerfactory type of the constructor overload, if we pass in the custom Icontrolleractivator type will run our, if not, the framework will use the default implementation type. You can also modify the custom controller factory defined in our previous article without much explanation.

In Figure 1 It is clear that the internal implementation of the Defaultcontrolleractivator type, described in the controller (a) article, wherein the idependencyresolver type of implementation class can be used to implement the controller injection, the way is almost similar to the above. The difference is that the injection of the Idependencyresolver type implementation can be global, which is the convenience that the framework provides to us.

The activation of the controller and the dynamic injection of the controller are all explained, and the next article has entered the MVC Filter series.

Jinyuan

Source: http://blog.csdn.net/jinyuan0829

This article is copyrighted by the author and Csdn, welcome reprint, but without the consent of the author must retain this statement, and on the article page

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.