ASP.NET MVC3 + Ninject.Mvc3 依赖注入原来可以这么简单
第一步、新創(chuàng)建一個(gè) ASP.NET MVC3 工程。
第二步、通過(guò)?NuGet?控制臺(tái)直接輸入命令:install-package Ninject.Mvc3
安裝完這個(gè)源碼包之后,所有的依賴注入框架已設(shè)置完成,無(wú)須你改動(dòng)任何代碼,
你會(huì)發(fā)現(xiàn)項(xiàng)目中添加了一個(gè)“App_Start”文件夾,在這個(gè)文件夾中生成了一個(gè)名為“NinjectMVC3.cs”的代碼文件。
第三步、打開(kāi) \App_Start\NinjectMVC3.cs,找到?RegisterServices 方法,將你的依賴注入映射代碼直接寫入即可。
?
代碼清單如下:
自動(dòng)生成的 \App_Start\NinjectMVC3.cs 代碼:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication3.App_Start.NinjectMVC3), "Start")][assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication3.App_Start.NinjectMVC3), "Stop")]
namespace MvcApplication3.App_Start
{
using System.Reflection;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Mvc;
using MvcApplication3.Services;
{
public static class NinjectMVC3
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
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();
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)
{
//寫入你需要綁定的依賴注入關(guān)系,除了此方法之外的其它代碼都是自動(dòng)生成的,無(wú)須我們操心。
//2011-10-26 新修復(fù) bug,遺漏了后面的 InRequestScope(),這個(gè)很重要!
//感謝 Scott Xu 南方小鬼 的指錯(cuò):),差點(diǎn)誤人子弟啊!
kernel.Bind<ITestService>().To<TestService>().InRequestScope();
}
}
}
測(cè)試代碼如下:?
\Services\ITestService.cs
namespace MvcApplication3.Services{
public interface ITestService
{
string GetMessage(string aString);
}
}
\Services\TestService.cs?
using System;namespace MvcApplication3.Services
{
public class TestService : ITestService
{
string ITestService.GetMessage(string aString)
{
return "--------------" + (String.IsNullOrEmpty(aString) ? String.Empty : aString) + "--------------";
}
}
}
\Controllers\HomeController.cs
using System.Web.Mvc;using Ninject;
using MvcApplication3.Services;
namespace MvcApplication3.Controllers
{
public class HomeController : Controller
{
[Inject]
public ITestService Service { get; set; }
public ActionResult Index()
{
ViewBag.Message = Service.GetMessage("Welcome to ASP.NET MVC!");
return View();
}
public ActionResult About()
{
return View();
}
}
}
原文:http://www.cnblogs.com/cnmaxu/archive/2011/10/25/2224375.html
轉(zhuǎn)載于:https://www.cnblogs.com/alphaqiu/archive/2012/03/01/2376309.html
總結(jié)
以上是生活随笔為你收集整理的ASP.NET MVC3 + Ninject.Mvc3 依赖注入原来可以这么简单的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Windows程序设计】Unicode
- 下一篇: 如何处理海量数据