AutoFachtml
AutoFac 在系統IOC中佔有很大做用,愈來愈多的項目中使用到AutoFac,可是國內很難看到完整的AutoFac介紹,因此 我打算 經過翻譯 AutoFac網站關於AutoFac的介紹、例子、相關擴展等來系統的介紹AutoFac,使更多的人更加了解並可以融會貫通的使用AutoFac。git
使用「英文-中文」雙語方式。github
首頁:http://autofac.org/windows
Autofac is an addictive Inversion of Control container for .NET 4.5, Silverlight 5, Windows Store apps, and Windows Phone 8 apps.api
Autofac 是一個爲.Net 4.5,Silverlight 5(銀光),Windows Store apps(Widnows商店應用)和Windows Phone 8 apps(windows手機應用)的使人激動上癮的IOC容器。app
Register Componentside
Build up containers with lambdas, types, or pre-built instances of components. You can also scan assemblies for registrations.模塊化
註冊組件(組成部分)學習
使用lambdas,類型,預建組件實例來構建容器。你依然可以經過掃描程序集來註冊。網站
var builder = new ContainerBuilder();
// Register individual components//註冊單獨的組件
builder.RegisterInstance(new TaskRepository)
.As<ITaskRepository>();
builder.RegisterType<TaskController>();
builder.Register(c => new LogManager(DateTime.Now))
.As<ILogger>();
// Scan an assembly for components//爲組件註冊程序集
builder.RegisterAssemblyTypes(myAssembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces();
var container = builder.Build();
Express Dependencies
Let Autofac inject your constructor parameters for you. It can also handle property and method injection.
表達式依賴
讓Auto注入到你的構造器參數。它也可以黑醋栗屬性和方法注入。
public class TaskController
{
private ITaskRepository _repository;
private ILogger _logger;
// Autofac will automatically find the registered
// values and pass them in for you.
public TaskController(
ITaskRepository repository,
ILogger logger)
{
this._repository = repository;
this._logger = logger;
}
}
Flexible Module System
Strike a balance between the deployment-time benefits of XML configuration and the power of code with Autofac modules.
靈活的模塊化系統
努力實現Xml配置部署的好處與使用Autoface Modules代碼的優點的平衡。
// Specify complex registrations in code
public class CarTransportModule : Module
{
public bool ObeySpeedLimit { get; set; }
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<Car>().As<IVehicle>();
if (ObeySpeedLimit)
builder.RegisterType<SaneDriver>().As<IDriver>();
else
builder.RegisterType<CrazyDriver>().As<IDriver>();
}
}
<!-- Change deployment-time behavior with XML -->
<autofac>
<module type="CarTransportModule">
<properties>
<property name="ObeySpeedLimit" value="true" />
</properties>
</module>
</autofac>
Simple Extension Points
Autofac provides activation events to let you know when components are being activated or released, allowing for a lot of customization with little code.
簡單的擴展點
Autofac 提供靈活的事件方便你知道組件是在何時被激活和被釋放的,容許使用較少的代碼實現大量的定製化。
var builder = new ContainerBuilder();
// Once a listener has been fully constructed and is
// ready to be used, automatically start listening.
builder.RegisterType<Listener>()
.As<IListener>()
.OnActivated(e => e.Instance.StartListening());
// When a processor is being constructed but before
// it's ready to be used, call an initialization method.
builder.RegisterType<Processor>()
.OnActivating(e => e.Instance.Initialize());
var container = builder.Build();
Want to download Autofac or learn more? Here's how.
想下載Autofac或者瞭解更多?從這裏開始
Download
The easiest way to get Autofac is through NuGet. We can generate a NuGet script for you or you can go through the NuGet Gallery.
下載
最簡單的獲取Autofac的方式是經過NuGet.咱們能夠爲你生成一NuGet腳本或者經過NuGet列表
Learn
If you're new to Autofac, the Quick Start guide is a good place to start. There's also an official documentation site, API documentation, and lots of info on the Autofac wiki. For questions, hit us up on StackOverflow.
學習
若是你是一個Autofac的初學者,快速入門指導是一個好的開始,有官方文檔站點,API文檔,在Autofac wiki上有不少信息。有問題,在StackOverflow上點擊咱們。
Get Involved
Found an issue? Let us know! Want to help us improve Autofac? Check out the source and our contributor's guide, and drop us a line on the discussion forum!
聯繫咱們