Some usage of the IOC pattern and container AUTOFAC in MVC

Source: Internet
Author: User

An example of IOC mode and AUTOFAC

One: For the IOC mode (control rollover) Simple understanding can refer to the autumn Wind blog, he introduced the container is unity,http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html

II: Let me introduce the application of IOC model and AUTOFAC in MVC in the project

1: Used for our company database is PG, so the corresponding also used NHibernate, if you use the database is SQL Server, equivalent to EF

First define the interface for data access:

1  Public InterfaceIrepository<tentity>whereTentity:entitybase2     {3         ObjectSave (TEntity entity);4 5         voidsaveorupdate (TEntity entity);6 7Iqueryable<tentity> Query (expression<func<tentity,BOOL>> predicate =NULL);8 9         /*Ten // <summary> One /// default ID int, sorted in ASC A // </summary> - pagedlist<tentity> Query (expression<func<tentity, bool>> predicate, int pageIndex, int PageS ize); -  the pagedlist<tentity> querydescending (expression<func<tentity, bool>> predicate, int pageIndex, int pageSize); -          * */ -  -         /*Best Practice TId*/ +TEntity Get (ObjectID); -  +TEntity Load (ObjectID); A  at         voidUpdate (TEntity entity); -         voidUpdate (action<tentity> Action, expression<func<tentity,BOOL>> predicate =NULL); -  -         voidDelete (TEntity entity); -         voidDelete (Expression<func<tentity,BOOL>> predicate =NULL); -  inIenumerable<tresult> sqlquery<tresult> (stringsql); -  to         intExecuteSQL (stringsql); +}

2: We implement these interfaces, where the session is equivalent to the context inside the EF

  Public classRepository<tentity>: irepository<tentity>whereTentity:entitybase {/** How to automatically package operations into a thing, such as insert update * There is a Submitchange method in LINQ to SQL, modified before a commit * (Injectiont) * * Single SQL (one-way) operation Good package * * Different repository use a session (one session per Request), how to ensure that in one thing together to mention         Repository can guarantee that all operations are handled in a transaction when the End Request commits the transaction what about the exception? * * should be a separate unitofwork, independent of each repository, and then all the transaction operations in which to complete **/        protectedISession Session {Get{returnsessionprovider.session;} }        Private ReadOnlyIsessionprovider Sessionprovider;  PublicRepository (Isessionprovider sessionprovider) { This. Sessionprovider =Sessionprovider; }         Public ObjectSave (TEntity entity) {returnSession.save (entity); }         Public voidsaveorupdate (TEntity entity) {session.saveorupdate (entity); }        /*Public TEntity Get (expression<func<tentity, bool>> predicate) {//Singleordefau LT is translated to ' limit 1 ' by NHibernate return Query (). Where (predicate).        Singleordefault (); }        * */         PublicIqueryable<tentity> Query (expression<func<tentity,BOOL>> predicate =NULL)        {            returnpredicate = =NULL? Session.query<tentity>(): Session.query<TEntity>().        Where (predicate); }        /*Public pagedlist<tentity> Query (expression<func<tentity, bool>> predicate, int. PageIndex, in T pageSize) {return Query (). Where (predicate). (Entity = entity). ID).        Topagedlist (PageIndex, pageSize); } public pagedlist<tentity> querydescending (expression<func<tentity, bool>> predicate, int pageIn Dex, int pageSize) {return Query (). Where (predicate). OrderByDescending (Entity = entity). ID).        Topagedlist (PageIndex, pageSize); }        * */         PublicTEntity Get (ObjectID) {returnSession.get<tentity>(ID); }         PublicTEntity Load (ObjectID) {returnSession.load<tentity>(ID); }         Public voidUpdate (TEntity entity) {session.update (entity); }         Public voidUpdate (action<tentity> Action, expression<func<tentity,BOOL>> predicate =NULL)        {            varentities =Query (predicate); foreach(varEntityinchentities)                {Action (entity);            Update (entity); }        }         Public voidDelete (TEntity entity) {Session.delete (entity); }         Public voidDelete (Expression<func<tentity,BOOL>> predicate =NULL)        {            varentities =Query (predicate); Entities.        ForEach (Delete); }         PublicIenumerable<tresult> sqlquery<tresult> (stringSQL) {            returnSession. Createsqlquery (SQL). Setscalars (typeof(TResult)). Setresulttransformer (Transformers.aliastobean (typeof(TResult))) . Future<TResult>(); }         Public intExecuteSQL (stringSQL) {            returnSession. Createsqlquery (SQL).        Executeupdate (); }

3: Now to really complete the dependency injection will have to ATUOFAC debut

(1): Use Builder. Registergeneric (typeof (Repository<>)). As (typeof (Irepository<>)). Instanceperrequest () registration or builder is also possible. Registertype<object> (). As<iobject> () registration, I am using the first, of course, can also be configured to use the way AUTOFAC, nor do the introduction

Using autofac;using webprint.data.repositories;namespace webprint.web.mvc.ioc{public    class Repositoriesmodule: Module    {        protected override void Load (Containerbuilder builder)        {            Builder. Registergeneric (typeof (Repository<>))                   . As (typeof (Irepository<>))                   . Instanceperrequest ();}}}    

(2) Register your controller class, must be cited as Autofac.Integration.Mvc.dll, written in a public IOC class

Using system.web.http;using system.web.mvc;using autofac;using autofac.integration.mvc;using  Autofac.integration.webapi;namespace webprint.web.mvc.ioc{public static class Iocconfig {public static void            Register (/*params assembly[] contorllerassemblies*/) {var builder = new Containerbuilder (); Builder.            Registermodule (New Repositoriesmodule ()); Register controller Builder. Registercontrollers (typeof (Mvcapplication).            Assembly); Register API Controller Builder. Registerapicontrollers (typeof (Mvcapplication).            Assembly); Register Filters//Global filters is not working builder.            Registerfilterprovider (); var container = Builder.            Build (); Configure Contollers with the dependency resolver dependencyresolver.setresolver (new Autofacdependencyresolv            ER (container)); Configure Web API with the dependency resolver GlobAlConfiguration.Configuration.DependencyResolver = new Autofacwebapidependencyresolver (container); }    }}

(3): Register call to Mvcapplication_start ()

 protected void Application_Start () {//AUTOFAC IoC Register iocconfig.register ();            Log4net Logconfig.register ();            Webapiconfig.register (globalconfiguration.configuration);            Globalconfiguration.configure (Webapiconfig.register);            Filterconfig.registerglobalfilters (globalfilters.filters);            Route registration Routeconfig.registerroutes (routetable.routes);            Remove the description of the MVC version in the header Mvchandler.disablemvcresponseheader = true;            var config = globalconfiguration.configuration; Config. Formatters.remove (config.            Formatters.xmlformatter); var jsonformatter = config. Formatters.oftype<jsonmediatypeformatter> ().            First ();            jsonFormatter.SerializerSettings.ReferenceLoopHandling = Referenceloophandling.ignore;            JsonFormatter.SerializerSettings.ContractResolver = new Nhibernatecontractresolver (); Jsonformatter.serializersettings. Converters.add (New Stringenumconverter ()); #if DEBUG//Start profile Nhibernateprofiler.startprofile R (); #endif}

(4): Inject the dependency code into the controller's constructor

namespace webprint.web.mvc.areas.order.controllers{[Routearea ("Order")] [Routeprefix ("Catalog")]/*mvc5 above the routing mechanism */  [Userauthorize (Roles = "Ordercatalog")]/* Permission control */public class Catalogordercontroller:controller {private        Iservice<model.order> OrderService;        Private iservice<importorder> Importorderservice; Public Catalogordercontroller (iservice<model.order> OrderService, iservice<importorder>            Importorderservice) {this.orderservice = OrderService;        This.importorderservice = Importorderservice;        } [Route ("{page:int}")]             Public ActionResult List (string strokeno, string Contractno,int page = 1) {Viewbag.strokeno = Strokeno;              Viewbag.contractno = Contractno;            Expression<func<importorder, bool>> expr = i = I.active = = 0; if (!strokeno.isnullorempty ()) {expr = expr. and (i = I.strokeno.touPper (). Contains (Strokeno.toupper ().            Trim ())); } if (!contractno.isnullorempty ()) {expr = expr. and (i = I.contractno.toupper (). Contains (Contractno.toupper ().            Trim ())); }//Increase press supply No filter (Vendorcode is supply no) var user = Userservice.load (usergrouppermission.cookieuser.            ID); var isadmin = user.            Groups.any (o = O.name = = Core.Group.RpacAdmin.ToString ()); var isregardless = user. Vendorcode.split (';').            Contains ("*"); if (!isadmin &&!isregardless) {if (!string. IsNullOrEmpty (user. Vendorcode) && user.                Vendorcode.contains (";")) {var supplierseries = user.}                    Vendorcode.split (';');                    Expression<func<importorder, bool>> expr1 = o = O.supplierseries.contains (supplierseries[0]); Supplierseries.foreach (o = = {Expr1 = expr1. or (or =). Supplierseries.containS (o));                    }); expr = expr.                and (EXPR1); } else {expr = expr. and (o = o.supplierseries.contains (user.                Vendorcode)); }}//call var model = importorderservice.queryable (expr). OrderByDescending (o=>o.id).            Topagedlist (page, 15); Viewbag.count = model.            Count ();        return View (model); }}

  

 

 

Some usage of the IOC pattern and container AUTOFAC in MVC

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.