6th MVC Basic Tools

Source: Internet
Author: User

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

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.