1.創(chuàng)建一個空白解決方案
2.添加一個類庫 名稱為XXX.Domain
3.添加一個ASP.MVC名稱為XXX.WebUI
4.選著空模版,勾選MVC核心引用
5.添加單元測試項目XXX.UntiTests
6.在程序包控制臺里面輸入以下代碼
復制代碼 代碼如下:
Install-Package Ninject -version 3.0.1.10 -projectname Toad.WebUI
Install-Package Ninject.Web.Common -version 3.0.0.7 -projectname Toad.WebUI
Install-Package Ninject.MVC3 -Version 3.0.0.6 -projectname Toad.WebUI
Install-Package Ninject -version 3.0.1.10 -projectname Toad.UnitTests
Install-Package Ninject.Web.Common -version 3.0.0.7 -projectname Toad.UnitTests
Install-Package Ninject.MVC3 -Version 3.0.0.6 -projectname Toad.UnitTests
Install-Package Moq -version 4.1.1309.1617 -projectname Toad.WebUI
Install-Package Moq -version 4.1.1309.1617 -projectname Toad.UnitTests
Install-Package Microsoft.Aspnet.Mvc -version 5.0.0 -projectname Toad.Domain
Install-Package EntityFramework -projectname Toad.Domain
Install-Package EntityFramework -projectname Toad.WebUI
7.添加引用依賴關系,WebUI-->Domain UnitTests--->WebUI UnitTests--->Domain.
8.創(chuàng)建文件夾Infrastructure,設置NinjectDependencyResolver
復制代碼 代碼如下:
namespace Toad.WebUI.Infrastructure
{
public class NinjectDependencyResolver : IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernelParam)
{
kernel = kernelParam;
AddBindings();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerableobject> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
private void AddBindings()
{
kernel.BindIProductRepository>().ToEFProductRepository>();
}
}
}
9.修改App_Start里面的NinjectWebCommon.cs文件
復制代碼 代碼如下:
System.Web.Mvc.DependencyResolver.SetResolver(new
Toad.WebUI.Infrastructure.NinjectDependencyResolver(kernel));
10.創(chuàng)建一個實體類
11.創(chuàng)建XXXXXRepository
12.最后新建Controller
13.生成模版視圖
以上我們就建立好了一個包含 Ninject框架 項目,是不是很簡單,希望小伙伴們喜歡,后續(xù)我們將繼續(xù)深入。
您可能感興趣的文章:- ASP.NET MVC使用Ninject的方法教程