zoukankan      html  css  js  c++  java
  • 【转】ASP.NET MVC 3 Service Location, Part 10: Controller Activator

    Controller Activator

    In MVC 1.0, we introduced IControllerFactory to allow better dependency injection of controller instances. We also provided theDefaultControllerFactory, which created controller instances with Activator.CreateInstance. Some of this is discussed in Part 2 of this series.

    We realized that DefaultControllerFactory was actually doing two things: turning the controller name into the controller type, and then instantiating the instance of that type. In ASP.NET MVC 3, we’ve split out the action of instantiating the controller instance into a new service: IControllerActivator. We have made the IControllerActivator instance findable via the dependency resolver.

    Disclaimer

    This blog post talks about ASP.NET MVC 3 Beta, which is a pre-release version. Specific technical details may change before the final release of MVC 3. This release is designed to elicit feedback on features with enough time to make meaningful changes before MVC 3 ships, so please comment on this blog post or contact me if you have comments.

    Implementing IControllerActivator

    The new IControllerActivator signature is identical to the DefaultControllerFactory.GetControllerInstance virtual method:

    public interface IControllerActivator {
        IController Create(RequestContext requestContext, Type controllerType);
    }

    Developers who previously implemented IControllerFactory by deriving from DefaultControllerFactory just to override the GetControllerInstance method for dependency injection purposes should now implement IControllerActivator instead.

    Location: IControllerActivator

    This is a “singly registered” style service introduced in MVC 3. There is no static registration point for this service as its purpose is strictly to support dependency injection; as such, the only way to register an instance of this service is through the dependency resolver.

    The logic in DefaultControllerFactory was updated to consult the dependency resolver, calling GetSerivce(typeof(IControllerActivator)) and using the provided service when present. If there is no IControllerActivator present in the dependency resolver, we will then ask the dependency resolver to create the concrete controller type by calling GetService(controllerType). If the dependency resolver also fails to create the concrete controller type, we finally fall back to the MVC 2 behavior of using Activator.CreateInstance to create the controller type.

    What’s Next?

    In the final post of this series, we will discuss the new View Page Activator service.

  • 相关阅读:
    从 LengthFieldBasedFrameDecoder 看 netty 处理拆包
    nacos 服务的实例变化
    nacos 中的服务和实例
    idea 编译 brooklin
    idea 编译 netty 源码
    consumeQueue 和 indexFile 文件
    rocketMQ 事务消息
    特殊符号存入mysql数据库时报错:Incorrect string value: 'xF0x9Fx98x84xF0x9F的解决方法
    面试中常用排序算法的python实现和性能分析
    获取编写代码所在文件的上级文件和上上级文件的相对路径
  • 原文地址:https://www.cnblogs.com/NickYao/p/1944821.html
Copyright © 2011-2022 走看看