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.
  • 相关阅读:
    C#验证码识别类网上摘抄的
    C#如何用WebClient动态提交文件至Web服务器和设定Http响应超时时间
    C#制作曲线图源码
    在PHP中怎样实现文件下载?
    ASP.NET如何调用Web Service
    MSDN中关于读取web.config的那块,System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString
    饿也要做一个和这个差不多的blog,但功能上有要增强的
    理解能力的高低决定人们的学习能力的高低
    有点困惑了,不知道是从smartClient入手还是从做网站web入手学习.net技术
    什么工厂模式?反射, 晕了,有书吗,推荐推荐.....5555555555555
  • 原文地址:https://www.cnblogs.com/student-note/p/7904663.html
Copyright © 2011-2022 走看看