博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOC运用到MVC中
阅读量:5298 次
发布时间:2019-06-14

本文共 5796 字,大约阅读时间需要 19 分钟。

IOC可以摒弃掉类中类的紧耦合,让设计和重用更简单,将IOC加入到MVC中的实现非常简单,那么有哪几种方法?它们的实现又是什么原理呢?

IOC在MVC中的注入,主要是在获取Controller对象中实现,也就是在获取Controller对象的方法中增加注入的相关实现就可以了,这些入口均是在DefaultControllerFactory类中,目前获取Controller的有如下方法:

(1)重写DefaultControllerFactory中的GetControllerInstance方法

源码:

1   protected internal virtual IController GetControllerInstance(RequestContext requestContext, Type controllerType) 2         { 3             if (controllerType == null) 4             { 5                 throw new HttpException(404, 6                                         String.Format( 7                                             CultureInfo.CurrentCulture, 8                                             MvcResources.DefaultControllerFactory_NoControllerFound, 9                                             requestContext.HttpContext.Request.Path));10             }11             if (!typeof(IController).IsAssignableFrom(controllerType))12             {13                 throw new ArgumentException(14                     String.Format(15                         CultureInfo.CurrentCulture,16                         MvcResources.DefaultControllerFactory_TypeDoesNotSubclassControllerBase,17                         controllerType),18                     "controllerType");19             }20             return ControllerActivator.Create(requestContext, controllerType);21         }

重写后:

public class DefaultFactoryIOC:DefaultControllerFactory    {        private IKernel kernel;        public DefaultFactoryIOC()        {            kernel = new StandardKernel();            kernel.Bind
().To
(); } protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) { return (IController)kernel.TryGet(controllerType); } }Global中定义 ControllerBuilder.Current.SetControllerFactory(new IOCTest.DefaultFactoryIOC());

(2)对前面的方法再一次观察发现,实例化Controller最后是交给了ControllerActivator.Create(requestContext, controllerType)方法,也就是如果能够实现或者重写这个方法就能实现注入,那么ControllerActivator来自于

public DefaultControllerFactory(IControllerActivator controllerActivator)            : this(controllerActivator, null, null)        {        }

接下来实现IControllerActivator这个接口,完成第二种IOC方法

public class ControllerActivatorIOC:IControllerActivator    {        private IKernel kernel;        public ControllerActivatorIOC()        {            kernel = new StandardKernel();            kernel.Bind
().To
(); } public IController Create(System.Web.Routing.RequestContext requestContext, Type controllerType) { return (IController)kernel.TryGet(controllerType); } }Global中 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); /*DefaultFactoryController*/ //ControllerBuilder.Current.SetControllerFactory(new IOCTest.DefaultFactoryIOC()); IControllerActivator controlleract = new IOCTest.ControllerActivatorIOC(); DefaultControllerFactory defaultControllerFactory = new DefaultControllerFactory(controlleract); ControllerBuilder.Current.SetControllerFactory(defaultControllerFactory); }

(3)接着最后一个方法仍然是从IControllerActivator 中,再DefaultContollerFactory这个Controller激活类中,默认调用实现IControllerActivator的类是DefaultControllerActivator,

它实现了以上这个Create方法

private Func
_resolverThunk; 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); } }

从上可知,Create方法最后会采用那个默认的构造或者第三方实现_resolverThunk的类来实现,而这个_resolverThunk就是从IDependencyResolver接口而来,所以实现IDependencyResolver就可以达到DI的目的

public class DependencyResolverIOC:IDependencyResolver    {        private IKernel kernel;        public DependencyResolverIOC()        {            kernel = new StandardKernel();            kernel.Bind
().To
(); } public object GetService(Type serviceType) { var obj = this.kernel.TryGet(serviceType); return obj; } public IEnumerable
GetServices(Type serviceType) { return this.kernel.GetAll(serviceType); } }
// Note: For instructions on enabling IIS6 or IIS7 classic mode,     // visit http://go.microsoft.com/?LinkId=9394801    public class MvcApplication : System.Web.HttpApplication    {        protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            /*DefaultFactoryController*/            //ControllerBuilder.Current.SetControllerFactory(new IOCTest.DefaultFactoryIOC());            //IControllerActivator controlleract = new IOCTest.ControllerActivatorIOC();            //DefaultControllerFactory defaultControllerFactory = new DefaultControllerFactory(controlleract);            //ControllerBuilder.Current.SetControllerFactory(defaultControllerFactory);            DependencyResolver.SetResolver(new IOCTest.DependencyResolverIOC());            WebApiConfig.Register(GlobalConfiguration.Configuration);            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);            RouteConfig.RegisterRoutes(RouteTable.Routes);        }    }

 

转载于:https://www.cnblogs.com/xiaoweinet/p/3142263.html

你可能感兴趣的文章
eclipse的调试方法的简单介绍
查看>>
OneAPM 云监控部署与试用体验
查看>>
加固linux
查看>>
wget 升级
查看>>
为什么需要大数据安全分析?
查看>>
day13.字典复习
查看>>
IPSP问题
查看>>
(转)Java中的String为什么是不可变的? -- String源码分析
查看>>
HNU 10362 A+B for Input-Output Practice (II)
查看>>
iOS——UIButton响应传参数
查看>>
【转帖】关于'eh vector constructor/destructor iterator'的讨论及类的内存分布模型
查看>>
十. 图形界面(GUI)设计9.列表和组合框
查看>>
10.17动手动脑
查看>>
操作系统实验一:并发程序设计
查看>>
互联网协议入门(一)
查看>>
Air Max 1 Men's Shoe Black/Team Red [NIKE-NO.12030]
查看>>
16_Python变量作用域_Python编程之路
查看>>
js 数组,字符串,json互相转换(在select实现多个输入的时候与后台交互常使用)...
查看>>
js index of()用法
查看>>
XSS原理及防范
查看>>