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 提供灵活的事件方便你知道组件是在什么时候被激活和被释放的,允许使用较少的代码实现大量的定制化。

 

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!

联系我们

发现了问题?让我们知道,想帮助我们改善Autofac?查看源代码和我们的贡献者指南,在论坛中给我们发信息。

你可能感兴趣的:(auto)