Use asp.net.4.5.1+mvc5.0 to build a Ninject framework project

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.