asp.net mvc 控制器的依赖注入(使用Ninject)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Ninject.Injection;
using Ninject;
using WebApplication1.Models;

namespace WebApplication1
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //控制器注入有3种方式
            /*
             1.通过默认的Controller
                //NinJectControllerFactory nc = new NinJectControllerFactory();
                //nc.Register<IPersonRepertory, PersonRepertory>();
                //ControllerBuilder.Current.SetControllerFactory(nc);
             2.通过自定义ControllerActivator
                NinJectActivator na = new NinJectActivator();
                na.Register<IPersonRepertory, PersonRepertory>();
                ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory(na));
             3.通过自定义IDepencyResolever
                NinJectDepencyResolever nj = new NinJectDepencyResolever();
                nj.Register<IPersonRepertory, PersonRepertory>();
                DependencyResolver.SetResolver(nj);
             */
        }
    }


    public class NinJectControllerFactory : DefaultControllerFactory
    {
        public IKernel Kernel { get; set; }
        public NinJectControllerFactory()
        {
            this.Kernel = new StandardKernel();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return (IController)this.Kernel.TryGet(controllerType);
        }
        public void Register<TForm, TTo>() where TTo : TForm
        {
            this.Kernel.Bind<TForm>().To<TTo>();
        }
    }
    public class NinJectActivator : IControllerActivator
    {
        public IKernel Kernel { get; set; }
        public NinJectActivator()
        {
            this.Kernel = new StandardKernel();
        }
        public void Register<TForm, TTo>() where TTo : TForm
        {
            this.Kernel.Bind<TForm>().To<TTo>();
        }
        public IController Create(RequestContext requestContext, Type controllerType)
        {
            return (IController)this.Kernel.TryGet(controllerType);
        }
    }


    public class NinJectDepencyResolever : IDependencyResolver
    {
        private IKernel kernel;
        public NinJectDepencyResolever()
        {
            kernel = new StandardKernel();
        }
        public object GetService(Type serviceType)
        {
           return kernel.TryGet(serviceType);
        }
        public IEnumerable<object> GetServices(Type serviceType)
        {
          return  kernel.GetAll(serviceType);
        }
        public void Register<TForm, TTo>() where TTo : TForm
        {
            this.kernel.Bind<TForm>().To<TTo>();
        }
    }
    //public class Temp : IControllerActivator
    //{
    //    public IController Create(RequestContext requestContext, Type controllerType)
    //    {
    //        return null;
    //    }
    //}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2017-11-27 16:16  夏风微凉  阅读(203)  评论(0编辑  收藏  举报