0
点赞
收藏
分享

微信扫一扫

Autofac正式发布2.1版

经过了2年多的开发,设计和试验,Autofac发布了第二版,针对1.4版本进行了重组,提供了更好的开发体验,你可以到这里下载正式的版本。 2.1版本也带来许多新特性: 组件发现:Autofac 2可...

​​Nicholas Blumhardt​​​经过了2年多的开发,设计和试验,Autofac发布了第二版,针对1.4版本进行了重组,提供了更好的开发体验,你可以到​​这里​​下载正式的版本。

2.1版本也带来许多新特性:

  1. 组件发现:Autofac 2可以从一个程序集的注册类型设置根据用户指定的规则: var dataAccess = Assembly.GetExecutingAssembly();
    builder.RegisterAssemblyTypes(dataAccess)
        .Where(t => t.Name.EndsWith("Repository"))
        .AsImplementedInterfaces();
    RegisterAssemblyTypes方法将Repository模式的数据访问接口类都注册了,语法非常的简单。
  2. 类型关系:Autofac通过自动支持小型,有重点,强类型的包装,来表达动态的依赖关系。类型关系如下:

Relationship

Adapter Type

Meaning

A needs a B

None

Dependency

A needs a B at some point in the future

​Lazy<B>​

Delayed instantiation

A needs a B until some point in the future

​Owned<B>​

Controlled lifetime

A needs to create instances of B

​Func<B>​

Dynamic instantiation

A provides parameters of types X and Y to B

​Func<X,Y,B>​

Parameterisation

A needs all the kinds of B

​IEnumerable<B>​

Enumeration

A needs to know X about B before using it

​Meta<T>​​​ and ​​Meta<B,X>​

Metadata interrogation

  1.    具体参看文章​​The Relationship Zoo​​.
  2. 组件元数据:.NET 4版本的Autofac 2.1支持类似于​​Managed Extensibility Framework​​​ (MEF)的功能。.NET 3.5(以及4.0)版本的提供了一个弱类型的​​Meta<T>​​类
  3. 集成​​Managed Extensibility Framework​​​ (MEF),具体内容参看​​Autofac MEF integration wiki page​​​,有一篇博客​​Hosting MEF Extensions in an IoC Container​​对这个架构进行了深入的讨论。

2.1版本不仅带来了许多新特性,而且对1.4版本也作了很大的改进:

  • 泛型现在是一等公民– Autofac 使用合适的泛型类型来解决通用的服务
  • 激活事件的参数实例是强类型的,例如 ​​builder.RegisterType<Foo>().OnActivating(e => e.Instance.Start())​​.
  • ASP.NET MVC的Controller注册更灵活和更简单,通过方法​​RegisterControllers()​​​ 进行注册,参看文章​​MVC Integration Changes in Autofac Beta 2.1.6.​​
  • 默认的Scope更改为单件(Singleton)而不是factory
  • 支持​​“Resolve Anything”​​
  • API 文档可以通过google进行搜索,例如​​http://www.google.com.au/search?q=site:api.autofac.org+ILifetimeScope​​。

 

参考文章

  • ​​Introducing Autofac 2.1 RTW​​
  • ​​http://code.google.com/p/autofac/wiki/NewInV2​​



举报

相关推荐

0 条评论