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);
    }
}

 

ASP.NET MVC Ninject 实现依赖注入,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。