zoukankan      html  css  js  c++  java
  • 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;
        //    }
        //}
    }

     

     

    Hold on, everything is possible.
  • 相关阅读:
    0309. Best Time to Buy and Sell Stock with Cooldown (M)
    0621. Task Scheduler (M)
    0106. Construct Binary Tree from Inorder and Postorder Traversal (M)
    0258. Add Digits (E)
    0154. Find Minimum in Rotated Sorted Array II (H)
    0797. All Paths From Source to Target (M)
    0260. Single Number III (M)
    0072. Edit Distance (H)
    0103. Binary Tree Zigzag Level Order Traversal (M)
    0312. Burst Balloons (H)
  • 原文地址:https://www.cnblogs.com/student-note/p/7904663.html
Copyright © 2011-2022 走看看