天天看點

Autofac

AutoFac

AutoFac 在系統IOC中占有很大作用,越來越多的項目中使用到AutoFac,但是國内很難看到完整的AutoFac介紹,是以 我打算 通過翻譯 AutoFac網站關于AutoFac的介紹、例子、相關擴充等來系統的介紹AutoFac,使更多的人更加了解并能夠融會貫通的使用AutoFac。

使用“英文-中文”雙語方式。

首頁:http://autofac.org/

Autofac is an addictive Inversion of Control container for .NET 4.5, Silverlight 5, Windows Store apps, and Windows Phone 8 apps.

Autofac 是一個為.Net 4.5,Silverlight 5(銀光),Windows Store apps(Widnows商店應用)和Windows Phone 8 apps(windows手機應用)的令人激動上瘾的IOC容器。

Register Components

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 提供靈活的事件友善你知道元件是在什麼時候被激活和被釋放的,允許使用較少的代碼實作大量的定制化。

// 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());

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!

聯系我們

發現了問題?讓我們知道,想幫助我們改善Autofac?檢視源代碼和我們的貢獻者指南,在論壇中給我們發資訊。