DI container, Unit Test framework, simulation tool
Notes:
1.Ninject conditional Binding Common methods:
When (predicate)//The binding is enforced when the predicate result is true. (predicate a lambda expression)
Whenclasshas<t> ()//when the injected class is annotated with an annotation attribute and its type is T, the binding is enforced ([T])
Wheninjectedinfo<t> ()//When the class to be injected is type T, the binding is enforced
2. Use Ninject:
1. Create a Dependency resolver (Infrastructure)
usingSystem;usingSystem.Collections.Generic;usingSYSTEM.WEB.MVC;usingEssentialtools.models;usingNinject;namespaceessentialtools.infrastructure{ Public classNinjdectdependencyresolver:idependencyresolver {PrivateIkernel kernel; PublicNinjdectdependencyresolver () {//creating an instance of the Ninject kernelKernel =NewStandardkernel (); Addbindings (); } Public ObjectGetService (Type servicetype) {returnkernel. Tryget (servicetype); } Publicienumerable<Object>getservices (Type servicetype) {returnkernel. GetAll (servicetype); } //establishing the relationship between an interface and an implementation class Private voidaddbindings () {//ConstructorsKernel. Bind<ivaluecalculator> (). To<linqvaluecalculator>(); //Properties//kernel. Bind<idiscoutnhelper> (). To<defaultdiscoutnhelper> (). Withpropertyvalue ("Discountsize", 20M); //constructor ParametersKernel. Bind<idiscoutnhelper> (). To<defaultdiscoutnhelper> (). Withconstructorargument ("Discountparam", 30M); //Conditional BindingKernel. Bind<idiscoutnhelper> (). To<flexiblediscounthelper> (). Wheninjectedinto<linqvaluecalculator>(); } }}
2. Registering a Dependency resolver (global.asax)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Http;usingSYSTEM.WEB.MVC;usingSystem.Web.Routing;usingessentialtools.infrastructure;namespaceessentialtools{//Note: For instructions on enabling IIS6 or IIS7 Classic mode,//please visithttp://go.microsoft.com/?LinkId=9394801 Public classMvcApplication:System.Web.HttpApplication {protected voidApplication_Start () {arearegistration.registerallareas (); //registering a Dependency parserDependencyresolver.setresolver (Newninjdectdependencyresolver ()); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); } }}
3. Refactoring Controller
.................
Private ivaluecalculator Calc; Public HomeController (ivaluecalculator calcparam) { = calcparam; } .................
Unit testing this is determined by the way of thinking and the style of development.
Source: http://yunpan.cn/cQ9QEgtrWXaS9 Access Password bbd2
6th MVC Basic Tools