zoukankan      html  css  js  c++  java
  • MVC三个IOC注入点之Ninject使用示例

    http://www.cnblogs.com/Raoh/archive/2013/03/27/AspNetMvc_IOC.html

    群里一个技术大牛说MVC有三个注入点,但我只会一个DefaultControllerFactory。 在群友的帮助下,我大致了解了下:

    IControllerFactory=>IDependencyResolver=>IControllerActivator 

    这三者的关系如下:

    其实从上面的关系可以看出来这三个注入点,相互都存在依赖关系。 我们还是老规矩上代码:

    1.IControllerFactory 注入:

        public class NInjectFactory:DefaultControllerFactory
        {
            private IKernel _iKernel;
            public NInjectFactory(IKernel ikernel)
            {
                this._iKernel = ikernel;
                AddBindHelper();
            }
            protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
            {
                return controllerType == null ? null : (IController)_iKernel.Get(controllerType);
            }
    
    
            public void AddBindHelper()
            {
    
                _iKernel.Bind(typeof(IProduct)).To(typeof(ProductService));
            }
        }

    2.IControllerActivator 注入:

        public class NinjectTwoControlActivator : IControllerActivator  
        {
            private IKernel _ikernel;
            public NinjectTwoControlActivator(IKernel ikernel)
            {
                this._ikernel = ikernel;
                AddBindHelper();
            }
    
            public IController Create(RequestContext requestContext, Type controllerType)
            {
                return controllerType == null ? null : (IController)_ikernel.Get(controllerType);
            }
    
            public void AddBindHelper()
            {
                _ikernel.Bind(typeof(IProduct)).To(typeof(ProductService));
            }
            
        }

    3.IDependencyResolver注入:

        public class NinjectThreeCotrolResolver:IDependencyResolver
        {
            private IKernel _ikernel;
            public NinjectThreeCotrolResolver(IKernel ikernel)
            {
                this._ikernel = ikernel;
                AddBindHelper();
            }
    
            #region IDependencyResolver Members
    
            public object GetService(Type serviceType)
            {
                try
                {
                    return _ikernel.Get(serviceType);
                }
                catch
                {
                    return null;
                }
            }
    
            public IEnumerable<object> GetServices(Type serviceType)
            {
                return Enumerable.Empty<object>();
            }
    
            #endregion
    
            public void AddBindHelper()
            {
                _ikernel.Bind(typeof(IProduct)).To(typeof(ProductService));
            }
        }

    三个在Global.asax的绑定到全局代码如下:

            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
    
                //ControllerBuilder.Current.SetControllerFactory(new NInjectFactory(new Ninject.StandardKernel()));//注册Ninject Ioc
    
                //var factory = new DefaultControllerFactory(new NinjectTwoControlActivator(new Ninject.StandardKernel()));
                //ControllerBuilder.Current.SetControllerFactory(factory);
    
                var dependencyResolver = new NinjectThreeCotrolResolver(new Ninject.StandardKernel());
                DependencyResolver.SetResolver(dependencyResolver);
                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }


    前端控制器实现方式统一如下:

        public class HomeController : Controller
        {
            private IProduct _iproduct;
            public HomeController(IProduct iproduct)
            {
                this._iproduct = iproduct;
            }
            public ActionResult Index()
            {
                var list = new List<Product>()
                {
                    new Product{ProductName="iphone4s",ProductPrice=3700},
                    new Product{ProductName="iphon5",ProductPrice=3400}
                };
                ViewBag.Price = _iproduct.GetAll(list);
                
                return View();
            }
    }

    利用构造函数从容器中取出来对应的服务,好了,非常感谢群里的的技术指导。非常感谢

  • 相关阅读:
    微信开发(5):公众号消息与事件推送处理(转)
    微信开发(4):微信第三方开放平台的搭建(转)
    微信开发(3):微信公众号发现金红包功能开发,利用第三方SDK实现(转)
    微信开发(2):微信js sdk分享朋友圈,朋友,获取config接口注入权限验证(转)
    微信开发(1) :网页授权获取用户的基本信息 实现微信登录(转)
    java实现创建临时文件然后在程序退出时自动删除文件(转)
    Linux定时对日志批量打包Shell脚本及定时任务crontab 详细用法
    httpd2.4出现AH00025: configuration error
    apache启动失败
    软件工程中的反面模式(anti-pattern)
  • 原文地址:https://www.cnblogs.com/flyfish2012/p/3282261.html
Copyright © 2011-2022 走看看