.NET IoC 容器(三)Autofac

目录

.NET IoC 容器(三)Autofac

Autofac

Autofac 是一个用于 .NET 应用程序的依赖注入 (Dependency Injection, DI) 容器。它帮助开发人员管理对象的创建和生命周期,使得依赖项的注入更加灵活和可维护。以下是 Autofac 的主要功能和特性概述:

依赖注入 (Dependency Injection)

Autofac 允许你通过构造函数、属性或方法注入依赖项。这样,你可以轻松地将对象的依赖关系传递给需要它们的类,从而提高代码的可测试性和可维护性。

模块化设计 (Modular Design)

Autofac 支持模块化设计,可以将相关的依赖项注册逻辑分组到模块中。这使得大型应用程序的配置更加简洁和易于管理。

生命周期管理 (Lifecycle Management)

Autofac 提供多种对象生命周期管理方式,例如单例 (Singleton)、每次请求 (Instance Per Dependency)、每个生命周期范围 (Instance Per Lifetime Scope) 等,允许你精确控制对象的创建和销毁时机。

注册与解析 (Registration and Resolution)

Autofac 通过流畅的 API 提供了多种注册组件的方式。你可以注册类型、实例、工厂方法,甚至通过扫描程序集自动注册组件。此外,Autofac 的解析功能支持构造函数参数注入、命名参数注入等高级特性。

支持 AOP (Aspect-Oriented Programming)

Autofac 与 AOP 框架(如 Castle Windsor)集成,支持横切关注点(如日志记录、事务管理)的处理,从而使业务逻辑代码更加简洁。

集成支持

Autofac 与许多流行的 .NET 库和框架集成良好,包括 ASP.NET Core、WCF、Web API、MVC 等。通过这些集成,Autofac 可以轻松地管理 web 应用程序中的依赖项。

扩展性

Autofac 具有很高的扩展性,可以通过自定义注册源、生命周期、解析器等方式扩展其功能,以满足特定应用的需求。

Nuget 安装

实现DI

定义接口

csharp 复制代码
internal interface IComputer
{
}
csharp 复制代码
internal interface IKeyboard
{
}
csharp 复制代码
internal interface IMouse
{
}
csharp 复制代码
internal interface IPerson
{
    IComputer Computer { get; set; }
    IKeyboard Keyboard { get; set; }
    IMouse Mouse { get; set; }
    void Work();
}

定义实现类

csharp 复制代码
internal class ConstructBase
{
    public ConstructBase() 
    {
        Console.WriteLine($"{this.GetType().Name} - 被构造了");
    }
}
csharp 复制代码
internal class LenovoComputer : ConstructBase, IComputer
{
    public LenovoComputer() : base() { }
}
csharp 复制代码
internal class TogarKeyboard : ConstructBase, IKeyboard
{
    public TogarKeyboard() : base() { }
}
csharp 复制代码
internal class LogitechMouse : ConstructBase, IMouse
{
    public LogitechMouse() : base() { }
}
csharp 复制代码
internal class Programmer : ConstructBase, IPerson
{
    public IComputer Computer { get; set; }
    public IKeyboard Keyboard { get; set; }
    public IMouse Mouse { get; set; }

    public Programmer(IComputer computer, IKeyboard keyboard, IMouse mouse) : base()
    {
        Computer = computer;
        Keyboard = keyboard;
        Mouse = mouse;
    }

    public void Work()
    {
        Console.WriteLine("Programmers are building software...");
    }
}

依赖注入

csharp 复制代码
var builder = new ContainerBuilder();
builder.RegisterType<LenovoComputer>().As<IComputer>();
builder.RegisterType<TogarKeyboard>().As<IKeyboard>();
builder.RegisterType<LogitechMouse>().As<IMouse>();
builder.RegisterType<Programmer>().As<IPerson>();
var container = builder.Build();
IPerson programmer = container.Resolve<IPerson>();
programmer.Work();

输出

Zhy.IoC.Autofac.LenovoComputer - 被构造了
Zhy.IoC.Autofac.TogarKeyboard - 被构造了
Zhy.IoC.Autofac.LogitechMouse - 被构造了
Zhy.IoC.Autofac.Programmer - 被构造了
Programmers are building software...

注入方式

构造函数注入 | 属性注入 | 方法注入

定义特性用来筛选属性注入

csharp 复制代码
public class InjectPropertyAttribute : Attribute
{

}

定义实现类

csharp 复制代码
public class Gamer : ConstructBase, IPerson
{
    public IComputer Computer { get; set; }
    [InjectProperty]
    public IKeyboard Keyboard { get; set; }
    public IMouse Mouse { get; set; }

    public Gamer(IComputer computer) : base()
    {
        Computer = computer;
    }

    public void Inject(IMouse mouse)
    {
        Mouse = mouse;
    }

    public void Work()
    {
        Console.WriteLine("The player is playing...");
    }
}

注入实现

csharp 复制代码
// 注入顺序(与编码顺序无关):构造函数注入 > 属性注入 > 方法注入
var builder = new ContainerBuilder();
builder.RegisterType<LenovoComputer>().As<IComputer>();
builder.RegisterType<TogarKeyboard>().As<IKeyboard>();
builder.RegisterType<LogitechMouse>().As<IMouse>();
builder.RegisterType<Gamer>()
    // 方法注入
    .OnActivating(e =>
    {
        IMouse mouse = e.Context.Resolve<IMouse>();
        e.Instance.Inject(mouse);
    })
    // 属性注入
    .PropertiesAutowired((g, o) => g.Name == "Keyboard")
    // 构造函数注入
    .UsingConstructor(typeof(IComputer))
    .As<IPerson>();
var container = builder.Build();
IPerson gamer = container.Resolve<IPerson>();
gamer.Work();

输出

Zhy.IoC.Autofac.LenovoComputer - 被构造了
Zhy.IoC.Autofac.Gamer - 被构造了
Zhy.IoC.Autofac.TogarKeyboard - 被构造了
Zhy.IoC.Autofac.LogitechMouse - 被构造了
The player is playing...

结果

注入顺序:构造函数注入 > 属性注入 > 方法注入

接口注册

重复注册

csharp 复制代码
var builder = new ContainerBuilder();
builder.RegisterType<LenovoComputer>().As<IComputer>();
builder.RegisterType<TogarKeyboard>().As<IKeyboard>();
builder.RegisterType<LogitechMouse>().As<IMouse>();
builder.RegisterType<Programmer>().As<IPerson>();
builder.RegisterType<Gamer>()
    // 方法注入
    .OnActivating(e =>
    {
        IMouse mouse = e.Context.Resolve<IMouse>();
        e.Instance.Inject(mouse);
    })
    // 属性注入
    .PropertiesAutowired((g, o) => g.Name == "Keyboard")
    // 构造函数注入
    .UsingConstructor(typeof(IComputer))
    .As<IPerson>();
var container = builder.Build();
IPerson programmer = container.Resolve<IPerson>();
programmer.Work();

输出

Zhy.IoC.Autofac.LenovoComputer - 被构造了
Zhy.IoC.Autofac.Gamer - 被构造了
Zhy.IoC.Autofac.TogarKeyboard - 被构造了
Zhy.IoC.Autofac.LogitechMouse - 被构造了
The player is playing...

调换Gamer和Programmer的注册顺序:

csharp 复制代码
var builder = new ContainerBuilder();
builder.RegisterType<LenovoComputer>().As<IComputer>();
builder.RegisterType<TogarKeyboard>().As<IKeyboard>();
builder.RegisterType<LogitechMouse>().As<IMouse>();
builder.RegisterType<Gamer>()
    // 方法注入
    .OnActivating(e =>
    {
        IMouse mouse = e.Context.Resolve<IMouse>();
        e.Instance.Inject(mouse);
    })
    // 属性注入
    .PropertiesAutowired((g, o) => g.Name == "Keyboard")
    // 构造函数注入
    .UsingConstructor(typeof(IComputer))
    .As<IPerson>();
builder.RegisterType<Programmer>().As<IPerson>();
var container = builder.Build();
IPerson programmer = container.Resolve<IPerson>();
programmer.Work();

输出

Zhy.IoC.Autofac.LenovoComputer - 被构造了
Zhy.IoC.Autofac.TogarKeyboard - 被构造了
Zhy.IoC.Autofac.LogitechMouse - 被构造了
Zhy.IoC.Autofac.Programmer - 被构造了
Programmers are building software...

结论

同一接口多次注册时,后注册的会覆盖前面注册的,若需要实现多重注册,需要指定名称

csharp 复制代码
var builder = new ContainerBuilder();
builder.RegisterType<LenovoComputer>().As<IComputer>();
builder.RegisterType<TogarKeyboard>().As<IKeyboard>();
builder.RegisterType<LogitechMouse>().As<IMouse>();
builder.RegisterType<Gamer>()
    // 方法注入
    .OnActivating(e =>
    {
        IMouse mouse = e.Context.Resolve<IMouse>();
        e.Instance.Inject(mouse);
    })
    // 属性注入
    .PropertiesAutowired((g, o) => g.Name == "Keyboard")
    // 构造函数注入
    .UsingConstructor(typeof(IComputer))
    .Named<IPerson>("person");
builder.RegisterType<Programmer>().Named<IPerson>("programmer");
var container = builder.Build();
IPerson programmer = container.ResolveNamed<IPerson>("programmer");
IPerson person = container.ResolveNamed<IPerson>("person");
programmer.Work();
person.Work();

输出

Zhy.IoC.Autofac.LenovoComputer - 被构造了
Zhy.IoC.Autofac.TogarKeyboard - 被构造了
Zhy.IoC.Autofac.LogitechMouse - 被构造了
Zhy.IoC.Autofac.Programmer - 被构造了
Zhy.IoC.Autofac.LenovoComputer - 被构造了
Zhy.IoC.Autofac.Gamer - 被构造了
Zhy.IoC.Autofac.TogarKeyboard - 被构造了
Zhy.IoC.Autofac.LogitechMouse - 被构造了
Programmers are building software...
The player is playing...

指定参数注册

修改接口

csharp 复制代码
internal interface IMouse
{
    string Type { get; set; }
}

修改实现类

csharp 复制代码
internal class LogitechMouse : ConstructBase, IMouse
{
    public LogitechMouse() : base() { }

    public LogitechMouse(string type) : base()
    {
        Type = type;
    }

    public string Type { get; set; }
}

注入

csharp 复制代码
var builder = new ContainerBuilder();
builder.RegisterType<LogitechMouse>().WithParameter("type","502").As<IMouse>();
var container = builder.Build();
IMouse mouse = container.Resolve<IMouse>();
Console.WriteLine("mouse type: " + mouse.Type);

输出

csharp 复制代码
Zhy.IoC.Autofac.LogitechMouse - 被构造了
mouse type: 502

生命周期

Instance Per Dependency(默认) 每次请求组件时都会创建一个新的实例。这是默认的生命周期管理模式。

csharp 复制代码
builder.RegisterType<MyService>().As<IMyService>();

Singleton 在整个容器生命周期内,只有一个实例。

csharp 复制代码
builder.RegisterType<MyService>().As<IMyService>().SingleInstance();

Instance Per Lifetime Scope 在每个生命周期范围内,共享一个实例。每个生命周期范围都会创建一个新的实例,但在该范围内共享该实例。

csharp 复制代码
builder.RegisterType<MyService>().As<IMyService>().InstancePerLifetimeScope();

Instance Per Matching Lifetime Scope 在特定的生命周期范围内共享实例,需要指定标记。

csharp 复制代码
builder.RegisterType<MyService>().As<IMyService>().InstancePerMatchingLifetimeScope("my-scope");

Instance Per Request 通常用于 web 应用程序,在每个 HTTP 请求期间共享一个实例。

csharp 复制代码
builder.RegisterType<MyService>().As<IMyService>().InstancePerRequest();

Externally Owned 组件由外部代码管理生命周期,Autofac 不会在容器释放时释放该实例。

csharp 复制代码
builder.RegisterType<MyService>().As<IMyService>().ExternallyOwned();

Instance Per Dependency 每次依赖请求时都会创建一个新实例。

csharp 复制代码
builder.RegisterType<MyService>().As<IMyService>().InstancePerDependency();

默认生命周期

csharp 复制代码
var builder = new ContainerBuilder();
builder.RegisterType<LenovoComputer>().As<IComputer>();
var container = builder.Build();
IComputer computer0 = container.Resolve<IComputer>();
IComputer computer1 = container.Resolve<IComputer>();
Console.WriteLine("computer0: " + computer0.GetHashCode());
Console.WriteLine("computer1: " + computer1.GetHashCode());
Console.WriteLine($"computer0 == computer1: {computer0 == computer1}");

输出

Zhy.IoC.Autofac.LenovoComputer - 被构造了
Zhy.IoC.Autofac.LenovoComputer - 被构造了
computer0: 32347029
computer1: 22687807
computer0 == computer1: False

单例生命周期

csharp 复制代码
var builder = new ContainerBuilder();
builder.RegisterType<LenovoComputer>().As<IComputer>().SingleInstance();
var container = builder.Build();
IComputer computer0 = container.Resolve<IComputer>();
IComputer computer1 = container.Resolve<IComputer>();
Console.WriteLine("computer0: " + computer0.GetHashCode());
Console.WriteLine("computer1: " + computer1.GetHashCode());
Console.WriteLine($"computer0 == computer1: {computer0 == computer1}");

输出

Zhy.IoC.Autofac.LenovoComputer - 被构造了
computer0: 32347029
computer1: 32347029
computer0 == computer1: True

每个周期范围一个生命周期

csharp 复制代码
var builder = new ContainerBuilder();
builder.RegisterType<LenovoComputer>().As<IComputer>().InstancePerLifetimeScope();
var container = builder.Build();
IComputer computer0 = container.Resolve<IComputer>();
using (var scope = container.BeginLifetimeScope())
{
    IComputer computer1 = scope.Resolve<IComputer>();
    IComputer computer2 = scope.Resolve<IComputer>();
    Console.WriteLine("computer0: " + computer0.GetHashCode());
    Console.WriteLine("computer1: " + computer1.GetHashCode());
    Console.WriteLine("computer2: " + computer2.GetHashCode());
    Console.WriteLine($"computer0 == computer1: {computer0 == computer1}");
    Console.WriteLine($"computer1 == computer2: {computer1 == computer2}");
}

输出

Zhy.IoC.Autofac.LenovoComputer - 被构造了
Zhy.IoC.Autofac.LenovoComputer - 被构造了
computer0: 32347029
computer1: 22687807
computer2: 22687807
computer0 == computer1: False
computer1 == computer2: True

依赖配置

Nuget

配置文件

Json

json 复制代码
{
  "components": [
    {
      "type": "Zhy.IoC.Autofac.LenovoComputer,Zhy.IoC.Autofac",
      "services": [
        {
          "type": "Zhy.IoC.Core.IComputer,Zhy.IoC.Core"
        }
      ],
      "autoActivate": true,
      "instanceScope": "singleinstance"
    },
    {
      "type": "Zhy.IoC.Autofac.TogarKeyboard,Zhy.IoC.Autofac",
      "services": [
        {
          "type": "Zhy.IoC.Core.IKeyboard,Zhy.IoC.Core"
        }
      ],
      "autoActivate": true,
      "instanceScope": "singleinstance"
    },
    {
      "type": "Zhy.IoC.Autofac.LogitechMouse,Zhy.IoC.Autofac",
      "services": [
        {
          "type": "Zhy.IoC.Core.IMouse,Zhy.IoC.Core"
        }
      ],
      "parameters": {
        "places": 4
      },
      "autoActivate": true,
      "instanceScope": "singleinstance"
    },
    {
      "type": "Zhy.IoC.Autofac.Programmer,Zhy.IoC.Autofac",
      "services": [
        {
          "type": "Zhy.IoC.Core.IPerson,Zhy.IoC.Core"
        }
      ],
      "autoActivate": true,
      "instanceScope": "singleinstance"
    }
  ]
}

XML

xml 复制代码
<?xml version="1.0" encoding="utf-8" ?>
<autofac>
	<components name="0">
		<type>Zhy.IoC.Autofac.LenovoComputer,Zhy.IoC.Autofac</type>
		<services name="0" type="Zhy.IoC.Core.IComputer,Zhy.IoC.Core" />
	</components>
	<components name="1">
		<type>Zhy.IoC.Autofac.TogarKeyboard,Zhy.IoC.Autofac</type>
		<services name="0" type="Zhy.IoC.Core.IKeyboard,Zhy.IoC.Core" />
	</components>
	<components name="2">
		<type>Zhy.IoC.Autofac.LogitechMouse,Zhy.IoC.Autofac</type>
		<services name="0" type="Zhy.IoC.Core.IMouse,Zhy.IoC.Core" />
	</components>
	<components name="3">
		<type>Zhy.IoC.Autofac.Programmer,Zhy.IoC.Autofac</type>
		<services name="0" type="Zhy.IoC.Core.IPerson,Zhy.IoC.Core" />
	</components>
</autofac>

调用

csharp 复制代码
var config = new ConfigurationBuilder();
config.AddJsonFile("DI-Autofac.json");
// config.AddXmlFile("DI-Autofac.xml");
var module = new ConfigurationModule(config.Build());
var builder = new ContainerBuilder();
builder.RegisterModule(module);
var container = builder.Build();
IPerson programmer = container.Resolve<IPerson>();
programmer.Work();

输出

Zhy.IoC.Autofac.LenovoComputer - 被构造了
Zhy.IoC.Autofac.TogarKeyboard - 被构造了
Zhy.IoC.Autofac.LogitechMouse - 被构造了
Zhy.IoC.Autofac.Programmer - 被构造了
Programmers are building software...

AOP

Nuget安装

定义接口、实现类

csharp 复制代码
public interface IMyService
{
    void DoWork();
}
csharp 复制代码
public void DoWork()
{
    Console.WriteLine("Doing work...");
}

定义切面

csharp 复制代码
using Castle.DynamicProxy;

public class LoggingInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine($"Invoking method {invocation.Method.Name} at {DateTime.Now}");
        invocation.Proceed();
        Console.WriteLine($"Method {invocation.Method.Name} has completed at {DateTime.Now}");
    }
}

容器配置

csharp 复制代码
var builder = new ContainerBuilder();
builder.Register(c => new LoggingInterceptor());
builder.RegisterType<MyService>()
       .As<IMyService>()
       .EnableInterfaceInterceptors()
       .InterceptedBy(typeof(LoggingInterceptor));
var container = builder.Build();
var service = container.Resolve<IMyService>();
service.DoWork();

输出

Invoking method DoWork at 2024/5/30 16:49:34
Doing work...
Method DoWork has completed at 2024/5/30 16:49:34

参考资料

IOC容器之Unity与AutoFac_unity autofac-CSDN博客

Autofac/src/Autofac at develop · autofac/Autofac (github.com)

控制容器的反转和依赖关系注入模式 (martinfowler.com)

相关推荐
向宇it7 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
数据的世界017 小时前
.NET开发人员学习书籍推荐
学习·.net
向宇it8 小时前
【从零开始入门unity游戏开发之——C#篇24】C#面向对象继承——万物之父(object)、装箱和拆箱、sealed 密封类
java·开发语言·unity·c#·游戏引擎
paixiaoxin9 小时前
CV-OCR经典论文解读|An Empirical Study of Scaling Law for OCR/OCR 缩放定律的实证研究
人工智能·深度学习·机器学习·生成对抗网络·计算机视觉·ocr·.net
坐井观老天13 小时前
在C#中使用资源保存图像和文本和其他数据并在运行时加载
开发语言·c#
pchmi15 小时前
C# OpenCV机器视觉:模板匹配
opencv·c#·机器视觉
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭17 小时前
C#都可以找哪些工作?
开发语言·c#
19004317 小时前
.NET重点
.net
m0_6632340117 小时前
在 .NET 5.0 运行 .NET 8.0 教程:使用 ASP.NET Core 创建 Web API
前端·asp.net·.net
boligongzhu18 小时前
Dalsa线阵CCD相机使用开发手册
c#