IoC~高效的Autofac


毫無疑問,微軟最青睞的IoC容器不是spring.net,unity而是Autofac,因為他的高效,因為他的簡潔,所以就邊微軟主導的orchard項目用的也是它,下面我用一個簡單

的實例來說明一個Autofac的用法。

 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數據源");
18         }
19 
20         #endregion
21     }
22     /// <summary>
23     /// 對redis數據源操作
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     }

我們現在去調用它一樣吧:

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //直接指定實例類型
 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             //通過配置文件實現對象的創建
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             //通過配置文件,配合Register方法來創建對象
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     }

怎么樣,搞簡單吧,下一講我將針對orchard項目,說說Autofac在具體項目中的使用。

現在看一下它的生命周期

1InstancePerDependency

對每一個依賴或每一次調用創建一個新的唯一的實例。這也是默認的創建實例的方式。

官方文檔解釋:Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.)

2InstancePerLifetimeScope

在一個生命周期域中,每一個依賴或調用創建一個單一的共享的實例,且每一個不同的生命周期域,實例是唯一的,不共享的。

官方文檔解釋: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.

3InstancePerMatchingLifetimeScope

在一個做標識的生命周期域中,每一個依賴或調用創建一個單一的共享的實例。打了標識了的生命周期域中的子標識域中可以共享父級域中的實例。若在整個繼承層次中沒有找到打標識的生命周期域,則會拋出異常: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.

4InstancePerOwned

在一個生命周期域中所擁有的實例創建的生命周期中,每一個依賴組件或調用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.

5SingleInstance

每一次依賴組件或調用Resolve()方法都會得到一個相同的共享的實例。其實就是單例模式。

官方文檔解釋:Configure the component so that every dependent component or call to Resolve() gets the same, shared instance.

6InstancePerHttpRequest

在一次Http請求上下文中,共享一個組件實例。僅適用於asp.net mvc開發。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM