• asp.net mvc 依赖注入Ninject


    1.安装Ninject

    2.使用Ninject

    一 安装Ninject

    Nuget:Ninject

    二 使用Ninject

     public interface IStudent
        {
            string GetName();
        }
     public class Student : IStudent
        {
            public string GetName()
            {
                return "ligenyun";
            }
        }
     public class NinjectControllerFactory : DefaultControllerFactory
        {
            private IKernel ninjectKernel;
    
            public NinjectControllerFactory()
            {
                ninjectKernel = new StandardKernel();
                AddBindings();
            }
    
            private void AddBindings()
            {
                ninjectKernel.Bind<IStudent>().To<Student>();
            }
    
            protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
            {
                return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
            }
        }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace WebApplicationNinject
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
            }
        }
    }
    using Ninject;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using WebApplicationNinject.Models;
    
    namespace WebApplicationNinject.Controllers
    {
        public class StudentController : Controller
        {
            IStudent calcImpl;
    
            public StudentController(IStudent _calcImpl)
            {
                calcImpl = _calcImpl;
            }
    
            // GET: Student
            public ActionResult Index()
            {
                ViewData["test"] = calcImpl.GetName();
                return View();
            }
        }
    }
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>@ViewData["test"]</h2>

  • 相关阅读:
    杀人蜂
    awk
    ggplot2
    非模式物种的注释
    org注释包
    NCB之taxonomy系列
    NCBI之gene系列
    notepad++查看二进制文件
    orthodb
    canu软件文献
  • 原文地址:https://www.cnblogs.com/ligenyun/p/10797053.html
Copyright © 2020-2023  润新知