Nicholas Blumhardt经过了2年多的开发,设计和试验,Autofac发布了第二版,针对1.4版本进行了重组,提供了更好的开发体验,你可以到这里下载正式的版本。
2.1版本也带来许多新特性:
var dataAccess = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(dataAccess)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces();
RegisterAssemblyTypes方法将Repository模式的数据访问接口类都注册了,语法非常的简单。
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 |
Meta<T>
类 2.1版本不仅带来了许多新特性,而且对1.4版本也作了很大的改进:
builder.RegisterType<Foo>().OnActivating(e => e.Instance.Start())
. RegisterControllers()
进行注册,参看文章MVC Integration Changes in Autofac Beta 2.1.6. 参考文章