Pro ASP. NET MVC 4, 4th edition Reading Note---part (1) DI

Source: Internet
Author: User
Tags ruby on rails

What I learned from Pro ASP 4 book

Open source of ASP.

Http://www.opensource.org/licenses/ms-pl.html

1. Introduce new trends of web development

node. js, Ruby on Rails,

2. Ninject

1) Ninject is used to de-couple classed, which known as DI dependency Inject. It is a DI container.

2) Adding Ninject to the Visual Studio Project

Open nuget->online-> Search for Ninject

3) How to use Ninject to implement DI

Implement Resolver

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSYSTEM.WEB.MVC;5 usingNinject;6 usingninject.parameters;7 usingNinject.syntax;8 usingSystem.Configuration;9 usingEssentialtools.models;Ten  One namespaceessentialtools.infrastructure A { -      Public classNinjectdependencyresolver:idependencyresolver -     { the         PrivateIkernel kernel; -  -          PublicNinjectdependencyresolver () -         { +Kernel =NewStandardkernel (); - addbindings (); +         } A  at          Public ObjectGetService (Type servicetype) -         { -             returnkernel. Tryget (servicetype); -         } -  -          Publicienumerable<Object>getservices (Type servicetype) in         { -             returnkernel. GetAll (servicetype); to         } +  -         Private voidaddbindings () the         { *Kernel. Bind<ivaluecalculator> (). To<linqvaluecalculator>(); $             //Create binding with property valuePanax NotoginsengKernel. Bind<idiscounthelper> (). To<defaultdiscounthelper> (). Withpropertyvalue ("discountsize", 50m); -              the             //Binding When specific condition happened +Kernel. Bind<idiscounthelper> (). To<flexiblediscounthelper>() A. Wheninjectedinto<linqvaluecalculator>(); the         } +     } -}
View Code

Inject Resolver into Global.asax

protected void Application_Start ()        {            arearegistration.registerallareas ();            Dependencyresolver.setresolver (new  ninjectdependencyresolver ());            Webapiconfig.register (globalconfiguration.configuration);            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);        }
View Code

Use DI Relationship in Controller

Private ivaluecalculator Calc;         //        // GET:/home/         Public HomeController (ivaluecalculator calcparam)        {            = calcparam;        }
View Code


3. Moq

It is a DLL used to unit testing

Pro ASP. NET MVC 4, 4th edition Reading Note---part (1) DI

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.