IOC practice--using AUTOFAC to realize MVC5.0 's IOC control inversion method

Source: Internet
Author: User

AUTOFAC is a. NET platform issued a good performance of the IOC framework, using it can achieve dependency injection and control inversion, so that the coupling between their software modules greatly reduced, so that software expansion, maintenance easier. Control inversion (inversion of the English abbreviation for IOC) is an important object-oriented programming principle to reduce the coupling problem of computer programs. I'll use AUTOFAC to implement the IOC control inversion method of ASP. mvc5.0. Here is the VS2013,AUTOFAC ASP. NET MVC 5 integration and MVC 5.0. AUTOFAC ASP 5 integration is AUTOFAC's MVC Encapsulation Library, and we only need very little code to implement dependency injection. See detailed examples below.

1. Download and install the relevant code right-click on your Web project to select "Manage NuGet packages"

Select online on the left and enter "AUTOFAC" in the Search input box

With AUTOFAC ASP. NET MVC 5 integration selected, click Install, so your project downloads AUTOFAC ASP 5 integration A reference to this related DLL and automatically adds the relevant reference.

2. Case presentation define a business interface Iproductrepository for all products
 Public Interface iproductrepository {        IQueryableget;}}

Define a business specific class to implement the Iproductrepository interface
 Public class efproductrepository:iproductrepository {        privatenew  efdbcontext ();          Public Iqueryable<product> Products        {            getreturn  context. Products; }         }}

Efdbcontext defines the EF class, I use the EF framework to implement reading database records, of course, you can use pure ADO, or other frameworks such as Ibatis.net, Nhibernate,dapper and so on. 2.1. Define a controller dependent Iproductrepository interface
 Public class  private public buycontroller (iproductrepository repo) {  = Repo ; }     // omit other code here }

2.2, binding Iproductrepository interface dependency of the specific implementation of the class
usingAUTOFAC;usingAUTOFAC.INTEGRATION.MVC;usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingWenMarine.BLL.Abstract;usingWenMarine.BLL.Concrete;usingWenMarine.Web.Infrastructure.Abstract;usingWenMarine.Web.Infrastructure.Concrete;namespacewenmarine.web.infrastructure{ Public classIocconfig { Public Static voidregisterdependencies () {varBuilder =NewContainerbuilder (); Builder. Registercontrollers (typeof(mvcapplication).           Assembly); Builder. Registertype<EFProductRepository> (). As<iproductrepository>(); //AUTOFAC Registration DependentIContainer container =Builder.            Build (); Dependencyresolver.setresolver (NewAutofacdependencyresolver (container)); }    }}

2.3. Call Iocconfig to make the dependency effective
protected void Application_Start () {arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (Bundletable.bundles); Iocconfig.registerdependencies ();}

3, summary through the above can be seen, buycontroller (high-level module) is a reference to a Iproductrepository abstract interface rather than the implementation of the specific Efproductrepository (specific module) class. This relies on inversion, that is, control inversion. The High-level module (buycontroller) does not depend on the underlying module (the low-layer code that reads the database operation inside the Efproductrepository), both of which are dependent on abstraction (iproductrepository) Abstraction is not dependent on specificity (efproductrepository), but on abstraction. The iproductrepository in this buycontroller is exactly which implementation, is registered by the Iocconfig class using AUTOFAC to register the binding. This way, if you want to change the database read mode, consider the performance problem, for example, without EF frame, to use dapper, directly create a new class dapperproductrepository inherit Iproductrepository interface, realize the products properties. Finally put the Iocconfig in Builder.registertype<efproductrepository> (). As<iproductrepository> (); Change to builder. Registertype<dapperproductrepository > (). As<iproductrepository> ();

You can do it. This buycontroller high-level code does not have to change at all, so that the expansion of development, to modify the closure (ocp-object-oriented open closure principle), this is the core of object-oriented programming. Very good loose coupling, the coupling between the modules greatly reduced, so that software expansion, maintenance easier.

Download

IOC practice--using AUTOFAC to realize MVC5.0 's IOC control inversion method

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.