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开发。

 

posted @ 2012-09-05 13:04  张占岭  阅读(26335)  评论(6编辑  收藏  举报