MVC三个IOC注入点之Ninject使用示例

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();
        }
}
复制代码

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值