ASP.NET MVC Ninject 实现依赖注入

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

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

ASP.NET MVC  Ninject 实现依赖注入
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);
    }
}
ASP.NET MVC  Ninject 实现依赖注入

 

ASP.NET MVC  Ninject 实现依赖注入

ASP.NET MVC Ninject 实现依赖注入,布布扣,bubuko.com

ASP.NET MVC Ninject 实现依赖注入

上一篇:(3)javascript 基本概念--- -- 数据类型


下一篇:.Net 文本框实现内容提示(仿Google、Baidu)