MVC中Autofac的使用

來源:互聯網
上載者:User

標籤:csdn   htm   手動   view   esc   route   pos   壓縮   load   

網上大佬的原地址:

https://www.cnblogs.com/liupeng/p/4806184.html

78227628?locationnum=2&fps=1

一、需要引用的檔案,使用Nuget安裝

第一個為Autofac

第二個為Autofac的MVC擴充

二、項目結構

測試使用的表名稱為SysSample,圖中2-6為其BLL、DAL、IBLL、IDAL、Models 項目。

三、關係

四、使用

1、建構函式注入

(1)自動注入。BLL、Controller  均是由Autofac統一注入

a.建立一個 統一被繼承的介面,IDependency

b.引用。BLL、IBLL、DAL、IDAL 四個項目均引用IDependency所在的Apps.AutofacBase項目。

c.繼承.。ISysSampleBLL和ISysSampleRepository均繼承此介面

 

d.建構函式。SysSampleBLL和SysSampleController建構函式修改。

e.Web項目的引用。Web需要添加對BLL、IBLL、DAL、IDAL四個項目的引用。否則自動注入時,缺少未引用項目的注入資訊。

f.Application_Start()方法的修改

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Web;using System.Web.Mvc;using System.Web.Optimization;using System.Web.Routing;using Apps.AutofacBase;using Apps.BLL;using Apps.DAL;using Apps.IBLL;using Apps.IDAL;using Autofac;using Autofac.Integration.Mvc;namespace Apps.Web{    public class MvcApplication : System.Web.HttpApplication    {        protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            RouteConfig.RegisterRoutes(RouteTable.Routes);            //啟用jscss壓縮            BundleTable.EnableOptimizations = true;            BundleConfig.RegisterBundles(BundleTable.Bundles);            #region 自動注入            //建立autofac管理註冊類的容器執行個體            var builder = new ContainerBuilder();            Assembly[] assemblies = Directory.GetFiles(AppDomain.CurrentDomain.RelativeSearchPath, "*.dll").Select(Assembly.LoadFrom).ToArray();            //註冊所有實現了 IDependency 介面的類型            Type baseType = typeof(IDependency);            builder.RegisterAssemblyTypes(assemblies)                   .Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract)                   .AsSelf().AsImplementedInterfaces()                   .PropertiesAutowired().InstancePerLifetimeScope();            //註冊MVC類型            builder.RegisterControllers(assemblies).PropertiesAutowired();            builder.RegisterFilterProvider();            var container = builder.Build();            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));            #endregion        }    }}
View Code

(2)手動注入

無需建立IDependency介面。

需要將所有需要注入的類、介面、調用者一一列出。

Application_Start()方法的修改

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Web;using System.Web.Mvc;using System.Web.Optimization;using System.Web.Routing;using Apps.AutofacBase;using Apps.BLL;using Apps.DAL;using Apps.IBLL;using Apps.IDAL;using Apps.Web.Controllers;using Autofac;using Autofac.Integration.Mvc;namespace Apps.Web{    public class MvcApplication : System.Web.HttpApplication    {        protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            RouteConfig.RegisterRoutes(RouteTable.Routes);            //啟用jscss壓縮            BundleTable.EnableOptimizations = true;            BundleConfig.RegisterBundles(BundleTable.Bundles);            #region 手動注入            //建立autofac管理註冊類的容器執行個體            var builder = new ContainerBuilder();                        /*需要進行依賴注入的IBLL*/            //類和介面的關係            builder.RegisterType<SysSampleRepository>().As<ISysSampleRepository>();                   builder.RegisterType<SysSampleBLL>().As<ISysSampleBLL>();            //調用者的注入。            builder.RegisterType<SysSampleBLL>().InstancePerDependency();//SysSampleBLL中調用了DAL層            //(不推薦)單個Controller控制器註冊            builder.RegisterType<SysSampleController>().InstancePerDependency();//SysSampleController調用的BLL層            //(推薦寫法)使用Autofac提供的RegisterControllers擴充方法來對程式集中所有的Controller一次性的完成註冊                                    //builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();            //產生具體的執行個體            var container = builder.Build();            //下面就是使用MVC的擴充 更改了MVC中的注入方式.            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));            #endregion        }    }}
View Code

2.屬性注入

(1)自動注入。

a.與1.(1) 建構函式注入的自動注入 配置,大部分一致,

b.需要修改的地方。注入調用者時,需要在最後面加上.PropertiesAutowired();

 

引用的對象,將建構函式修改為屬性。

 (2)手動注入

a.與2.(1) 同理,大部分一致

b.需要修改的地方。百度上說注入調用者時,需要在最後面加上.PropertiesAutowired();

但是在實際調用時,仍然調用失敗。未進行深入研究。

五、備忘

為書寫和觀看方便,當前使用的是。自動注入+屬性注入。

MVC中Autofac的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.