.NET手记-Autofac入门Getting Started

内容主要翻译自官方文档,原文请看:http://autofac.readthedocs.org/en/latest/getting-started/index.html#application-startup

将Autofac集成进你的应用的基本模式:

  • 在脑海中构造基于控制反转(IoC)的应用程序架构
  • 添加Autofac引用.
  • 在应用启动配置流程...
  • 创建一个 ContainerBuilder.
  • 注册组件(components).
  • build定义的ContainerBuilder生成Autofac容器,并存储它以供后续使用.
  • 在程序运行时...
  • 从Autofac容器(container)创建生命周期域(lifetime scope).
  • 使用生命周期域来解析出组件实例.

这篇指导将会使用一个控制台程序一步一步演示如何使用Autofac. 一旦你有了基础的了解,可以到以下wiki站点查看更高级的使用方法和技巧: integration information for WCF, ASP.NET, and other application types.

 

构建应用架构

 

基于IoC的想法应该是对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它,而不是把所有类绑在一起,每次都要通过依赖new来传递实例.如果你想了解更多请看:Martin Fowler has an excellent article explaining dependency injection/inversion of control.

对于我们的示例应用程序,我们将定义一个输出当前日期的类。但是,我们不希望它和控制台绑在一起,因为我们希望以后能够测试它或用在一个没有控制台程序的地方。
我们还会去尽量抽象输出日期的机制,以便于我们后续重复使用。
我们会做这样的事情:

using System;

namespace DemoApp
{
  // This interface helps decouple the concept of
  // "writing output" from the Console class. We
  // don't really "care" how the Write operation
  // happens, just that we can write.
  public interface IOutput
  {
    void Write(string content);
  }

  // This implementation of the IOutput interface
  // is actually how we write to the Console. Technically
  // we could also implement IOutput to write to Debug
  // or Trace... or anywhere else.
  public class ConsoleOutput : IOutput
  {
    public void Write(string content)
    {
      Console.WriteLine(content);
    }
  }

  // This interface decouples the notion of writing
  // a date from the actual mechanism that performs
  // the writing. Like with IOutput, the process
  // is abstracted behind an interface.
  public interface IDateWriter
  {
    void WriteDate();
  }

  // This TodayWriter is where it all comes together.
  // Notice it takes a constructor parameter of type
  // IOutput - that lets the writer write to anywhere
  // based on the implementation. Further, it implements
  // WriteDate such that today's date is written out;
  // you could have one that writes in a different format
  // or a different date.
  public class TodayWriter : IDateWriter
  {
    private IOutput _output;
    public TodayWriter(IOutput output)
    {
      this._output = output;
    }

    public void WriteDate()
    {
      this._output.Write(DateTime.Today.ToShortDateString());
    }
  }
}

 

添加Autofac引用

 

首先添加Autofac引用到你的项目,这个例子中我们仅添加Autofac Core引用,其他类型应用程序可能会用到不同的Autofac.Integration类库。

通过NuGet可以很容易为项目引入引用,如图:

 

add reference

 

应用启动(Application Startup)

 

在🈺️启动时,你需要创建一个ContainerBuilder并使用它注册各种组件. 组件可以是表达式、.NET类型或者一些用于暴露内部服务的代码。

简单的来说,有个实现了接口的类型,如下:

 

public class SomeType : IService
{
}

 

你可以通过两种方式定位到它:通过类型SomeType,或者通过接口IService.

在这个例子中,组件是SomeType,同时暴露的服务是指SomeType类型和IService接口。

在Autofac中,你应该通过ContainerBuilder来注册它们。

 

// Create your builder.
var builder = new ContainerBuilder();

// Usually you're only interested in exposing the type
// via its interface:
builder.RegisterType<SomeType>().As<IService>();

// However, if you want BOTH services (not as common)
// you can say so:
builder.RegisterType<SomeType>().AsSelf().As<IService>();

 

对于我们的示例应用程序,我们需要注册我们所有的组件(类),并暴露他们的服务(接口)。

我们还需要存储容器,这样它之后可用于解析出各种类型实例。

 

using System;
using Autofac;

namespace DemoApp
{
  public class Program
  {
    private static IContainer Container { get; set; }

    static void Main(string[] args)
    {
      var builder = new ContainerBuilder();
      builder.RegisterType<ConsoleOutput>().As<IOutput>();
      builder.RegisterType<TodayWriter>().As<IDateWriter>();
      Container = builder.Build();

      // The WriteDate method is where we'll make use
      // of our dependency injection. We'll define that
      // in a bit.
      WriteDate();
    }
  }
}

 

这样我们就拥有了一个注册了所有组件的容器,这些组件同时暴露了它们的服务。现在让我们来使用这个容器。

 

应用运行时

 

在应用运行期间,你需要从容器生命周期域中解析出组件实例来使用它们。容器自身就是一个生命周期域(lifetime scope),技术上来讲,你可以直接从容器中解析出需要的组件。

但是我们不推荐直接操作容器,这会导致内存泄漏。

当我们解析出一个组件时,依赖于我们定义的lifetime scope,一个新的对象实例会被创建。其中一些组件可能需要被释放,例如实现IDispose接口的对象。当生命周期域被释放时,Autofac可以自动处理组件资源的释放。

但是容器存在于整个应用程序生命周期。如果你直接从容器中解析出大量组件,可能会导致很多组件一直等待被释放。这样做法并不好,通常会导致内存泄露。

替代方案是,每次我们从容器来创建子生命周期域,。当你从子域完成解析组件后,子域将会被释放,同时所有资源都会被清理掉。

对于我们的示例程序来说,我们将会从子域中解析出IDateWriter实例,并运行WriterDate()方法。同时在我们完成后,子域将会被释放。

 

namespace DemoApp
{
  public class Program
  {
    private static IContainer Container { get; set; }

    static void Main(string[] args)
    {
      // ...the stuff you saw earlier...
    }

    public static void WriteDate()
    {
      // Create the scope, resolve your IDateWriter,
      // use it, then dispose of the scope.
      using (var scope = Container.BeginLifetimeScope())
      {
        var writer = scope.Resolve<IDateWriter>();
        writer.WriteDate();
      }
    }
  }
}

 

现在当你运行你的程序时...

  • WriteDate()方法从Autofac中请求IDateWriter实例.
  • Autofac发现IDateWriter可以映射到TodayWriter类型,所以创建了一个TodayWriter实例.
  • Autofac发现TodayWriter在它的构造函数中需要一个IOutput实例.
  • Autofac发现IOutput 可以映射到ConsoleOutput,所以创建了一个ConsoleOutput新实例.
  • Autofac使用ConsoleOutput实例完成构造一个TodayWriter实例.
  • Autofac返回了一个完全构造好的TodayWriter实例来用于“WriteDate” 方法.

之后,如果你的程序想要实现输出不同日期, 你可以构造不同实现IDateWriter接口的类型,同时在应用启动时注册. 你不必改变其他任何的类. 这就是控制反转!

 

注意:正常来说service location很大程度上被认为是反模式。也就是说,到处手动创建域(scope)和大量使用容器不一定是最好的方案。使用Autofac你通常不必做我们在示例应用中的做的事情。组件会从一个应用的中心或者顶层来解析,手写的解决方案很罕见的。当然,如何设计应用程序是由你自己决定的。

posted @ 2016-01-06 16:35  msp的昌伟哥哥  阅读(2679)  评论(2编辑  收藏  举报