This article mainly introduces the use of asp.net.4.5.1+mvc5.0 to build a Ninject framework project method, the need for friends can refer to the
1. Create a Blank Solution
2. Add a class library name called Xxx.domain
3. Add a ASP.MVC name called Xxx.webui
4. Select the empty template, tick the MVC core reference
5. Add Unit Test Project Xxx. Untitests
6. Enter the following code in the package console
Copy code code as follows:
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. Add Reference dependencies , Webui-->domain unittests--->webui unittests--->domain.
8. Create folder Infrastructure , set Ninjectdependencyresolver
The
code is as follows:
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 ienumerable<object> getservices (Type servicetype)
{
return kernel. GetAll (servicetype);
}
private void Addbindings ()
{
kernel. Bind<iproductrepository> (). To<efproductrepository> ();
}
}
}
9. Modify the NinjectWebCommon.cs file inside the App_start
The code is as follows:
System.Web.Mvc.DependencyResolver.SetResolver (New
Toad.WebUI.Infrastructure.NinjectDependencyResolver (kernel));
10. Create an entity class
11. Create Xxxxxrepository
12. The last new controller
13. Generate Template View
Above we have built a Ninject framework project, is not very simple, I hope the small partners like, follow-up we will continue to deepen.