日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET MVC3 + Ninject.Mvc3 依赖注入原来可以这么简单

發布時間:2025/3/13 asp.net 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC3 + Ninject.Mvc3 依赖注入原来可以这么简单 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一步、新創建一個 ASP.NET MVC3 工程。

第二步、通過?NuGet?控制臺直接輸入命令:install-package Ninject.Mvc3

安裝完這個源碼包之后,所有的依賴注入框架已設置完成,無須你改動任何代碼,

你會發現項目中添加了一個“App_Start”文件夾,在這個文件夾中生成了一個名為“NinjectMVC3.cs”的代碼文件。

第三步、打開 \App_Start\NinjectMVC3.cs,找到?RegisterServices 方法,將你的依賴注入映射代碼直接寫入即可。

?

代碼清單如下:

自動生成的 \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)
{
//寫入你需要綁定的依賴注入關系,除了此方法之外的其它代碼都是自動生成的,無須我們操心。
//2011-10-26 新修復 bug,遺漏了后面的 InRequestScope(),這個很重要!
//感謝 Scott Xu 南方小鬼 的指錯:),差點誤人子弟啊!

kernel.Bind<ITestService>().To<TestService>().InRequestScope();
}
}
}

測試代碼如下:?

\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



轉載于:https://www.cnblogs.com/alphaqiu/archive/2012/03/01/2376309.html

總結

以上是生活随笔為你收集整理的ASP.NET MVC3 + Ninject.Mvc3 依赖注入原来可以这么简单的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。