IoC~高效(xiao)的Autofac
毫無疑問,微軟(ruan)最(zui)青睞(lai)的(de)IoC容器不是(shi)spring.net,unity而是(shi)Autofac,因(yin)為他的(de)高效(xiao),因(yin)為他的(de)簡潔,所以就邊微軟(ruan)主(zhu)導的(de)orchard項目用的(de)也(ye)是(shi)它,下(xia)面我用一個簡單
的實例來說(shuo)明一(yi)個Autofac的用法(fa)。
1 /// <summary> 2 /// DB Operate Interface 3 /// </summary> 4 public interface IRepository 5 { 6 void Get(); 7 } 8 /// <summary> 9 /// 對SQL數據源操作 10 /// </summary> 11 public class SqlRepository : IRepository 12 { 13 #region IRepository 成員 14 15 public void Get() 16 { 17 Console.WriteLine("sql數(shu)據(ju)源(yuan)"); 18 } 19 20 #endregion 21 } 22 /// <summary> 23 /// 對(dui)redis數(shu)據源操作 24 /// </summary> 25 public class RedisRepository : IRepository 26 { 27 #region IRepository 成員 28 29 public void Get() 30 { 31 Console.WriteLine("Redis數據源"); 32 } 33 34 #endregion 35 } 36 /// <summary> 37 /// 數據源基類 38 /// </summary> 39 public class DBBase 40 { 41 public DBBase(IRepository iRepository) 42 { 43 _iRepository = iRepository; 44 } 45 public IRepository _iRepository; 46 public void Search(string commandText) 47 { 48 _iRepository.Get(); 49 } 50 }
我們(men)現在去調用它一(yi)樣吧:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //直接指定實例類(lei)型 6 var builder = new ContainerBuilder(); 7 builder.RegisterType<DBBase>(); 8 builder.RegisterType<SqlRepository>().As<IRepository>(); 9 using (var container = builder.Build()) 10 { 11 var manager = container.Resolve<DBBase>(); 12 manager.Search("SELECT * FORM USER"); 13 } 14 15 //通過配置文件(jian)實(shi)現(xian)對象的創建(jian) 16 var builder2 = new ContainerBuilder(); 17 builder2.RegisterType<DBBase>(); 18 builder2.RegisterModule(new ConfigurationSettingsReader("autofac")); 19 using (var container = builder2.Build()) 20 { 21 var manager = container.Resolve<DBBase>(); 22 manager.Search("SELECT * FORM USER"); 23 } 24 //通(tong)過(guo)配置文(wen)件,配合Register方法來創建對象(xiang) 25 var builder3 = new ContainerBuilder(); 26 builder3.RegisterModule(new ConfigurationSettingsReader("autofac")); 27 builder3.Register(c => new DBBase(c.Resolve<IRepository>())); 28 using (var container = builder3.Build()) 29 { 30 var manager = container.Resolve<DBBase>(); 31 manager.Search("SELECT * FORM USER"); 32 } 33 34 Console.ReadKey(); 35 } 36 }
怎么樣,搞簡單吧,下一講我將(jiang)針對orchard項(xiang)目(mu),說(shuo)說(shuo)Autofac在具體(ti)項(xiang)目(mu)中(zhong)的使用。
現在看一下它的生命周期
1、InstancePerDependency
對每(mei)一(yi)個依(yi)賴或每(mei)一(yi)次調用創建(jian)一(yi)個新的(de)唯(wei)一(yi)的(de)實(shi)(shi)例。這也是默(mo)認的(de)創建(jian)實(shi)(shi)例的(de)方(fang)式(shi)。
官方文檔解釋:Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.)
2、InstancePerLifetimeScope
在一(yi)個(ge)生命周(zhou)期域中,每(mei)一(yi)個(ge)依(yi)賴或調用創建一(yi)個(ge)單一(yi)的(de)共享的(de)實(shi)例(li),且每(mei)一(yi)個(ge)不(bu)同的(de)生命周(zhou)期域,實(shi)例(li)是(shi)唯一(yi)的(de),不(bu)共享的(de)。
官方文檔解釋:Configure the component so that every dependent component or call to Resolve() within a single ILifetimeScope gets the same, shared instance. Dependent components in different lifetime scopes will get different instances.
3、InstancePerMatchingLifetimeScope
在一個做標識的生命周期域中,每一個依賴或調用創建一個單一的共享的實例。打了標識了的生命周期域中的子標識域中可以共享父級域中的實例。若在整個繼承層次中沒有找到打標識的生命周期域,則會拋出異常:DependencyResolutionException。
官方文檔解釋:Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope tagged with any of the provided tags value gets the same, shared instance. Dependent components in lifetime scopes that are children of the tagged scope will share the parent's instance. If no appropriately tagged scope can be found in the hierarchy an DependencyResolutionException is thrown.
4、InstancePerOwned
在一個生命周期域中所擁有的實例創建的生命周期中,每一個依賴組件或調用Resolve()方法創建一個單一的共享的實例,并且子生命周期域共享父生命周期域中的實例。若在繼承層級中沒有發現合適的擁有子實例的生命周期域,則拋出異常:DependencyResolutionException。
官方文檔解釋:
Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope created by an owned instance gets the same, shared instance. Dependent components in lifetime scopes that are children of the owned instance scope will share the parent's instance. If no appropriate owned instance scope can be found in the hierarchy an DependencyResolutionException is thrown.
5、SingleInstance
每一次依賴組件或調用Resolve()方法都會得(de)到(dao)一(yi)個(ge)相同的共享的實例。其實就是單例模式。
官方文檔解釋:Configure the component so that every dependent component or call to Resolve() gets the same, shared instance.
6、InstancePerHttpRequest
在一次Http請求上下文中,共享一個組件實例。僅適用于asp.net mvc開發。