• ASP.NET MVC Ninject 实现依赖注入


    刚写了一篇Unity实现依赖注入,下载了一个Ninject的demo源码,研究了一下,发现了,基本就是换汤不换药,直接上代码,便于以后理解

    源地址:http://www.cnblogs.com/sixiangqimeng/p/3572018.html                       

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using BLL;
    using MODEL;
    
    namespace webUI.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Home/
            Iservice service;
    
            public HomeController(Iservice _service)
            {
                service = _service;
            }
    
            public ActionResult Index()
            {
                model m = new model();
                m.name = "111";
                m.id = 1;
                service.uname(m);
                service.itemid(m);
                ViewBag.name = m.name;
                ViewBag.id = m.id;
                return View();
            }
    
        }
    }
    
    
    [assembly: WebActivator.PreApplicationStartMethod(typeof(webUI.App_Start.NinjectWebCommon), "Start")]
    [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(webUI.App_Start.NinjectWebCommon), "Stop")]
    
    namespace webUI.App_Start
    {
        using System;
        using System.Web;
    
        using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    
        using Ninject;
        using Ninject.Web.Common;
    
        public static class NinjectWebCommon 
        {
            private static readonly Bootstrapper bootstrapper = new Bootstrapper();
    
            /// <summary>
            /// Starts the application
            /// </summary>
            public static void Start() 
            {
                DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
                DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
                bootstrapper.Initialize(CreateKernel);
            }
            
            /// <summary>
            /// Stops the application.
            /// </summary>
            public static void Stop()
            {
                bootstrapper.ShutDown();
            }
            
            /// <summary>
            /// Creates the kernel that will manage your application.
            /// </summary>
            /// <returns>The created kernel.</returns>
            private static IKernel CreateKernel()
            {
                var kernel = new StandardKernel();
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
                
                RegisterServices(kernel);
                return kernel;
            }
    
            /// <summary>
            /// Load your modules or register your services here!
            /// </summary>
            /// <param name="kernel">The kernel.</param>
            private static void RegisterServices(IKernel kernel)
            {
                kernel.Bind<DAL.Iuser>().To<DAL.user>();
                kernel.Bind<DAL.Iitem>().To<DAL.item>();
    
                kernel.Bind<BLL.Iservice>().To<BLL.service>();
            }        
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DAL;
    using MODEL;
    
    namespace BLL
    {
        public class service:Iservice
        {
    
            #region Iservice Members
    
            private Iuser user;
            private Iitem item;
    
            public service(Iuser _user, Iitem _item)
            {
                user = _user;
                item = _item;
            }
    
            public void uname(model m)
            {
                user.uname(m);
            }
    
            public void itemid(model m)
            {
                item.itemid(m);
            }
    
            #endregion
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MODEL;
    
    namespace BLL
    {
        public interface Iservice
        {
            void uname(model m);
            void itemid(model m);
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MODEL;
    
    namespace DAL
    {
        public class item:Iitem
        {
            #region Iitem Members
    
            public void itemid(model m)
            {
                m.id = m.id + 10000;
            }
    
            #endregion
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MODEL;
    
    namespace DAL
    {
        public interface Iitem
        {
            void itemid(model m);
        }
    }

  • 相关阅读:
    flowable流程中心设计之http监听器(十)
    flowable流程中心设计之自定义sql(十)
    flowable流程中心设计之相关表(十六)
    Spring Boot源码阅读自动化Starter原理(四)
    flowable流程中心设计之是否是发起人节点(十四)
    flowable流程中心设计之自动完成任务(十三)
    Spring源码系列导航
    设计思路判断是否有修改数据
    flowable流程中心设计之流程进度图扩展(十五)
    树莓派4B安装 百度飞桨paddlelite 做视频检测 (一、环境安装)
  • 原文地址:https://www.cnblogs.com/sixiangqimeng/p/3572018.html
Copyright © 2020-2023  润新知