日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

依赖注入容器Autofac的详解[转]

發(fā)布時間:2025/3/14 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 依赖注入容器Autofac的详解[转] 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

依賴注入容器Autofac的詳解

發(fā)表于 renfengbin 分享到:GMAIL郵箱???????? Hotmail郵箱 delicious digg

Autofac和其他容器的不同之處是它和C#語言的結(jié)合非常緊密,在使用過程中對你的應(yīng)用的侵入性幾乎為零,更容易與第三方的組件集成,并且開源,Autofac的主要特性如下:

1,靈活的組件實(shí)例化:Autofac支持自動裝配,給定的組件類型Autofac自動選擇使用構(gòu)造函數(shù)注入或者屬性注入,Autofac還可以基于lambda表達(dá)式創(chuàng)建實(shí)例,這使得容器非常靈活,很容易和其他的組件集成。 2,資源管理的可視性:基于依賴注入容器構(gòu)建的應(yīng)用程序的動態(tài)性,意味著什么時候應(yīng)該處理那些資源有點(diǎn)困難。Autofac通過容器來跟蹤組件的資源管理。對于不需要清理的對象,例如Console.Out,我們調(diào)用ExternallyOwned()方法告訴容器不用清理。細(xì)粒度的組件生命周期管理:應(yīng)用程序中通常可以存在一個應(yīng)用程序范圍的容器實(shí)例,在應(yīng)用程序中還存在大量的一個請求的范圍的對象,例如一個HTTP請求,一個IIS工作者線程或者用戶的會話結(jié)束時結(jié)束。通過嵌套的容器實(shí)例和對象的作用域使得資源的可視化。 3,Autofac的設(shè)計(jì)上非常務(wù)實(shí),這方面更多是為我們這些容器的使用者考慮: ●組件侵入性為零:組件不需要去引用Autofac。 ●靈活的模塊化系統(tǒng):通過模塊化組織你的程序,應(yīng)用程序不用糾纏于復(fù)雜的XML配置系統(tǒng)或者是配置參數(shù)。 ●自動裝配:可以是用lambda表達(dá)式注冊你的組件,autofac會根據(jù)需要選擇構(gòu)造函數(shù)或者屬性注入 ●XML配置文件的支持:XML配置文件過度使用時很丑陋,但是在發(fā)布的時候通常非常有用

Autofac的簡單使用,并加入了Repository模式.

定義兩個簡單實(shí)體類: public class Persion {public string Name { get; set; }public int Age { get; set; } }public class Custom {public string CustomName { get; set; }public int CustomID { get; set; } }

?

定義泛型數(shù)據(jù)庫訪問接口: public interface Idal<T> where T:class {void Insert(T entity);void Update(T entity);void Delete(T entity); }

?

泛型數(shù)據(jù)庫訪問接口的泛型實(shí)現(xiàn): public class Dal<T>:Idal<T> where T : class {#region Idal<T> Memberspublic void Insert(T entity){HttpContext.Current.Response.Write("您添加了一個:"+entity.GetType().FullName);}public void Update(T entity){HttpContext.Current.Response.Write("您更新一個:"+entity.GetType().FullName);}public void Delete(T entity){HttpContext.Current.Response.Write("您刪除了一個:"+entity.GetType().FullName);}#endregion }

?

使用Repository模式實(shí)現(xiàn)訪問。

//Repository的泛型接口: public interface IRepository<T> where T:class {void Insert(T entity);void Update(T entity);void Delete(T entity); }//Repository泛型接口的泛型實(shí)現(xiàn): public class Repository<T>:IRepository<T> where T:class {private Idal<T> _dal;public Repository(Idal<T> dal){_dal = dal;}#region IRepository<T> Memberspublic void Insert(T entity){_dal.Insert(entity);}public void Update(T entity){_dal.Update(entity);}public void Delete(T entity){_dal.Delete(entity);}#endregion }

?

IDependency的依賴接口,不需要任何方法體,所有的業(yè)務(wù)對象都實(shí)現(xiàn)該接口 public interface IDependency { }

?

實(shí)現(xiàn)IDependency接口的CustomBll類,通過Repository模式存儲數(shù)據(jù)。 public class CustomBll:IDependency {private readonly IRepository<Custom> _repository;public CustomBll(IRepository<Custom> repository){_repository = repository;}public void Insert(Custom c){_repository.Insert(c);}public void Update(Custom c){_repository.Update(c);}public void Delete(Custom c){_repository.Delete(c);} }

?

實(shí)現(xiàn)IDependency接口的PersionBll類,通過Repository模式存儲數(shù)據(jù)。
public class PersionBll:IDependency {private readonly IRepository<Persion> _repository;public PersionBll(IRepository<Persion> repository){_repository = repository;}public void Insert(Persion p){_repository.Insert(p);}public void Update(Persion p){_repository.Update(p);}public void Delete(Persion p){_repository.Delete(p);} }

?

下面編寫組件實(shí)例化測試

var builder = new ContainerBuilder(); builder.RegisterGeneric(typeof(Dal<>)).As(typeof(Idal<>)).InstancePerDependency(); builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency(); builder.Register(c=>new PersionBll((IRepository<Persion>)c.Resolve(typeof(IRepository<Persion>)))); builder.Register(c => new CustomBll((IRepository<Custom>)c.Resolve(typeof(IRepository<Custom>))));//var container = builder.Build()教程里都是使用這行代碼, //我本地測試需要加入ContainerBuildOptions枚舉選項(xiàng)。 using (var container = builder.Build(ContainerBuildOptions.None)) {// var repository= container.Resolve(typeof(IRepository<Persion>),new TypedParameter());// IRepository<Persion> _repository = repository as Repository<Persion>;// var m = new PersionBll(_repository);Persion p = new Persion();p.Name = "小人";p.Age = 27;var m = container.Resolve<PersionBll>();m.Insert(p);Custom c = new Custom();c.CustomName = "小小";c.CustomID = 10;var cc = container.Resolve<CustomBll>();cc.Update(c); }

這里通過ContainerBuilder方法RegisterGeneric對泛型類進(jìn)行注冊(當(dāng)然也可以通過ContainerBuilder方法RegisterType對不是泛型的類進(jìn)行注冊),當(dāng)注冊的類型在相應(yīng)得到的容器中可以Resolve你的類實(shí)例。 builder.RegisterGeneric(typeof(Dal<>)).As(typeof(Idal<>)).InstancePerDependency();通過AS可以讓類中通過構(gòu)造函數(shù)依賴注入類型相應(yīng)的接口。(當(dāng)然也可以使用builder.RegisterType<類>().As<接口>();來注冊不是泛型的類 ) Build()方法生成一個對應(yīng)的Container實(shí)例,這樣,就可以通過Resolve解析到注冊的類型實(shí)例。

注:如果要獲得某個泛型的實(shí)例,需要將泛型T代表的類傳進(jìn)去。如上c.Resolve(typeof(IRepository<Persion>))返回的是Object,需要轉(zhuǎn)換為響應(yīng)的接口。

當(dāng)然可以使用autofac的新特性RegisterAssemblyTypes,從一個程序集的注冊類型設(shè)置根據(jù)用戶指定的規(guī)則,例子如下:

var builder = new ContainerBuilder(); builder.RegisterGeneric(typeof(Dal<>)).As(typeof(Idal<>)).InstancePerDependency(); builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency(); //上面的那些類如果在單獨(dú)的工程里,如生成的程序集為AutofacUnitTest,就可以使用 //Assembly.Load("AutofacUnitTest")獲得響應(yīng)的程序集。如果所有的文件在一個控制臺程序里, //可以通過Assembly.GetExecutingAssembly(); 直接獲得相應(yīng)的程序集。 Assembly dataAccess = Assembly.Load("AutofacUnitTest"); builder.RegisterAssemblyTypes(dataAccess).Where(t => typeof(IDependency).IsAssignableFrom(t) && t.Name.EndsWith("Bll")); //RegisterAssemblyTypes方法將實(shí)現(xiàn)IDependency接口并已Bll結(jié)尾的類都注冊了,語法非常的簡單。

?

此條目發(fā)表在 開源 分類目錄。將固定鏈接加入收藏夾。

轉(zhuǎn)載于:https://www.cnblogs.com/shengfa/p/4022893.html

總結(jié)

以上是生活随笔為你收集整理的依赖注入容器Autofac的详解[转]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。