Autofac 容器
Autofac 是一款.NET IoC 容器 . 它管理类之间的依赖关系, 从而使 应用在规模及复杂性增长的情况下依然可以轻易地修改 . 它的实现方式是将常规的.net类当做组件
处理.
- 安装 NuGet 程序包:
Autofac 6.0.0
- 创建一个 ContainerBuiler
- 注册接口和实现关系
- 通过 ContainerBuiler 的 Build 方法,得到 IContainer 容器
- 通过 IContainer 容器获取实例
- 使用服务
ITestServiceA
和TestServiceA
public interface ITestServiceA
{
void Show();
}
public class TestServiceA : ITestServiceA
{
public TestServiceA()
{
Console.WriteLine($"{this.GetType().Name} 被构造了...");
}
public void Show()
{
Console.WriteLine($"This is a {this.GetType().Name} Instance...");
}
}
- Program 中的
Main
方法
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
var container = builder.Build();
// 获取服务实例
var testService = container.Resolve<ITestServiceA>();
testService.Show();
Autofac 多种注入方式
ITestServiceB
和TestServiceB
public interface ITestServiceB
{
void Show();
}
public class TestServiceB : ITestServiceB
{
private ITestServiceA _testServiceA;
public void SetService(ITestServiceA testServiceA)
{
_testServiceA = testServiceA;
}
public TestServiceB()
{
Console.WriteLine($"{this.GetType().Name} 被构造了...");
}
public void Show()
{
// _testServiceA.Show();
Console.WriteLine($"This is a {this.GetType().Name} Instance...");
}
}
ITestServiceC
和TestServiceC
public interface ITestServiceC
{
void Show();
}
public class TestServiceC : ITestServiceC
{
public TestServiceC()
{
Console.WriteLine($"{this.GetType().Name} 被构造了...");
}
public void Show()
{
Console.WriteLine($"This is a {this.GetType().Name} Instance...");
}
}
ITestServiceD
和TestServiceD
public interface ITestServiceD
{
void Show();
}
public class TestServiceD : ITestServiceD
{
public ITestServiceA TestServiceA { get; set; }
public ITestServiceB TestServiceB { get; set; }
public ITestServiceC TestServiceC { get; set; }
public TestServiceD()
{
Console.WriteLine($"{this.GetType().Name} 被构造了...");
}
public void Show()
{
// TestServiceA.Show();
// TestServiceB.Show();
// TestServiceC.Show();
Console.WriteLine($"This is a {this.GetType().Name} Instance...");
}
}
- 构造函数注入
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
builder.RegisterType<TestServiceB>().As<ITestServiceB>();
builder.RegisterType<TestServiceC>().As<ITestServiceC>();
builder.RegisterType<TestServiceD>().As<ITestServiceD>();
var container = builder.Build();
// 获取服务实例
var testService = container.Resolve<ITestServiceA>();
testService.Show();
- 属性注入
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
builder.RegisterType<TestServiceB>().As<ITestServiceB>();
builder.RegisterType<TestServiceC>().As<ITestServiceC>();
builder.RegisterType<TestServiceD>().As<ITestServiceD>().PropertiesAutowired();
var container = builder.Build();
// 获取服务实例
var testService = container.Resolve<ITestServiceD>();
testService.Show();
- 方法注入
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
builder.RegisterType<TestServiceB>().OnActivated(e =>
e.Instance.SetService(e.Context.Resolve<ITestServiceA>())
).As<ITestServiceB>();
builder.RegisterType<TestServiceC>().As<ITestServiceC>();
builder.RegisterType<TestServiceD>().As<ITestServiceD>();
var container = builder.Build();
// 获取服务实例
var testService = container.Resolve<ITestServiceB>();
testService.Show();
Autofac 生命周期
- InstancePerDependency :默认模式,每次调用,都会重新实例化对象;每次请求都创建一个新的对象;
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerDependency();
var container = builder.Build();
var testServiceA = container.Resolve<ITestServiceA>();
var testServiceA1 = container.Resolve<ITestServiceA>();
Console.WriteLine(object.ReferenceEquals(testServiceA,testServiceA1));
- SingleInstance :单例模式,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>().SingleInstance();
var container = builder.Build();
var testServiceA = container.Resolve<ITestServiceA>();
var testServiceA1 = container.Resolve<ITestServiceA>();
Console.WriteLine(object.ReferenceEquals(testServiceA,testServiceA1));
- InstancePerLifetimeScope : 同一个生命周期域中,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;且每个不同的生命周期域中的实例是唯一的,不共享的。
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerLifetimeScope();
var container = builder.Build();
ITestServiceA testServiceA15;
ITestServiceA testServiceA16;
using (var scope1 = container.BeginLifetimeScope())
{
var testServiceA11 = scope1.Resolve<ITestServiceA>();
var testServiceA12 = scope1.Resolve<ITestServiceA>();
Console.WriteLine(object.ReferenceEquals(testServiceA11,testServiceA12));
testServiceA15 = testServiceA12;
}
using (var scope1 = container.BeginLifetimeScope())
{
var testServiceA13 = scope1.Resolve<ITestServiceA>();
var testServiceA14 = scope1.Resolve<ITestServiceA>();
Console.WriteLine(object.ReferenceEquals(testServiceA13,testServiceA14));
testServiceA16 = testServiceA14;
}
Console.WriteLine(object.ReferenceEquals(testServiceA15,testServiceA16));
- InstancePerMatchingLifetimeScope : 同一个匹配的生命周期域中,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;且每个不匹配的生命周期域中的实例是唯一的,不共享的。
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>()
.InstancePerMatchingLifetimeScope("Run2948");
var container = builder.Build();
ITestServiceA testServiceA15;
ITestServiceA testServiceA16;
using (var scope1 = container.BeginLifetimeScope("Run2948"))
{
var testServiceA11 = scope1.Resolve<ITestServiceA>();
using (var scope2 = container.BeginLifetimeScope())
{
var testServiceA12 = scope2.Resolve<ITestServiceA>();
Console.WriteLine(object.ReferenceEquals(testServiceA11,testServiceA12));
}
testServiceA15 = testServiceA11;
}
using (var scope1 = container.BeginLifetimeScope("Run2948"))
{
var testServiceA13 = scope1.Resolve<ITestServiceA>();
using (var scope2 = container.BeginLifetimeScope())
{
var testServiceA14 = scope2.Resolve<ITestServiceA>();
Console.WriteLine(object.ReferenceEquals(testServiceA13,testServiceA14));
}
testServiceA16 = testServiceA13;
}
Console.WriteLine(object.ReferenceEquals(testServiceA15,testServiceA16));
InstancePerOwned : 在一个所拥有的实例创建的生命周期中,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;(较少使用)
InstancePerHttpRequest : 同一次Http请求上下文中,每次调用,都会使用同一个实例化的对象;每次都用同一个对象;仅适用于 ASP.NET (CORE) MVC 或 WebForm 应用程序
Autofac 支持配置文件
- 安装 NuGet 程序包:
Autofac.Extensions.DependencyInjection 7.1.0
、Autofac.Configuration 6.0.0
- 新建配置文件(指定接口和实现的对应关系)
autofac.json
:
{
"components":[
{
"type: "One.Services.TestServiceA,One",
"services": [
{
"type": "One.Services.ITestServiceA,One"
}
],
"instanceScope": "single-instance",
"injectProperties": true
},
{
"type: "One.Services.TestServiceB,One",
"services": [
{
"type": "One.Services.ITestServiceB,One"
}
],
"instanceScope": "single-instance",
"injectProperties": true
},
{
"type: "One.Services.TestServiceC,One",
"services": [
{
"type": "One.Services.ITestServiceC,One"
}
],
"instanceScope": "single-instance",
"injectProperties": true
},
{
"type: "One.Services.TestServiceD,One",
"services": [
{
"type": "One.Services.ITestServiceD,One"
}
],
"instanceScope": "single-instance",
"injectProperties": true
}
]
}
- 读取配置文件,完成服务对应关系的注册
var builder = new ContainerBuilder();
var config = new ConfigurationBuilder();
var configSource = new JsonConfigurationSource()
{
Path = "Config/autofac.json",
Optional = false,
ReloadOnChange = true
};
config.Add(configSource);
var configModule = new ConfigurationModule(config.Build());
builder.RegisterModule(configModule);
var container = builder.Build();
// 获取服务实例
var testServiceA = container.Resolve<ITestServiceA>();
var testServiceD = container.Resolve<ITestServiceD>();
testServiceD.Show();
- 新建 ITestServiceA 的新版实现类
TestServiceUpdate
public class TestServiceUpdate : ITestServiceA
{
public TestServiceUpdate()
{
Console.WriteLine($"{this.GetType().Name} 被构造了...");
}
public void Show()
{
Console.WriteLine($"This is a {this.GetType().Name} Instance...");
}
}
- 通过修改配置文件
autofac.json
来实现快速实现 ITestServiceA 的实现的重新定义:
{
"components":[
{
"type: "One.Services.TestServiceUpdate,One",
"services": [
{
"type": "One.Services.ITestServiceA,One"
}
],
"instanceScope": "single-instance",
"injectProperties": true
},
// ...
Autofac 整合 .NET 5 MVC
安装 NuGet 程序包:
Autofac.Extensions.DependencyInjection 7.1.0
Program文件中指定 Autofac 工厂替换默认工厂:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).UseServiceProviderFactory(new AutofacServiceProviderFactory());
- 在 Startup 类中增加 ConfigureContainer 方法:
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
}
- 通过控制器构造函数注入,获取实例
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly ITestServiceA _serviceA;
public ValuesController(ITestServiceA serviceA
{
_serviceA = serviceA;
}
[HttpGet]
public IActionResult Get()
{
_serviceA.Show();
return Ok();
}
}
- 使用 IServiceCollection 注册的服务,将来也会交给 Autofac 管理
public void ConfigureServices(IServiceCollection services)
{
#region IServiceCollection 注册的服务,将来也会交给 Autofac 处理
services.AddTransient<ITestServiceA, TestServiceA>();
services.AddTransient<ITestServiceB, TestServiceB>();
services.AddTransient<ITestServiceC, TestServiceC>();
#endregion
}
public void ConfigureContainer(ContainerBuilder builder)
{
// builder.RegisterType<TestServiceA>().As<ITestServiceA>();
// builder.RegisterType<TestServiceB>().As<ITestServiceB>();
// builder.RegisterType<TestServiceC>().As<ITestServiceC>();
builder.RegisterType<TestServiceD>().As<ITestServiceD>();
}
Autofac 支持控制器属性注入
控制器本身是一个类,它的实例其实是有 IControllerActivator 来创建的。
- 指定控制器的实例由容器来创建
public void ConfigureServices(IServiceCollection services)
{
// ...
#region 指定控制器的实例由容器来创建
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
#endregion
}
- 注册控制器的抽象和具体的关系
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<TestServiceA>().As<ITestServiceA>().PropertiesAutowired();
builder.RegisterType<TestServiceB>().As<ITestServiceB>();
#region 注册所有控制器的关系及控制器实例化所需要的组件
var controllersTypesInAssembly = typeof(Startup).Assembly.GetExportedTypes()
.Where(type => typeof(ControllerBase).IsAssignableFrom(type)).ToArray();
builder.RegisterTypes(controllersTypesInAssembly)
.PropertiesAutowired();
#endregion
}
- 在控制器内定义属性
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
public ITestServiceA TestServiceA { get; set; }
public ITestServiceB TestServiceB { get; set; }
[HttpGet]
public IActionResult Get()
{
TestServiceA.Show();
TestServiceB.Show();
return Ok();
}
}
- 扩展:自己控制哪些属性需要做依赖注入(默认是让控制器中的属性都依赖注入)
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class AutowaredAttribute : Attribute { }
public class PropertySelector : IPropertySelector
{
public bool InjectProperty(PropertyInfo propertyInfo, object instance)
{
return propertyInfo.CustomAttributes.Any(ca => ca.AttributeType == typeof(AutowaredAttribute));
}
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
builder.RegisterType<TestServiceB>().As<ITestServiceB>();
#region 注册所有控制器的关系及控制器实例化所需要的组件
var controllersTypesInAssembly = typeof(Startup).Assembly.GetExportedTypes()
.Where(type => typeof(ControllerBase).IsAssignableFrom(type)).ToArray();
builder.RegisterTypes(controllersTypesInAssembly)
.PropertiesAutowired(new PropertySelector());
#endregion
}
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[Autowared]
public ITestServiceA TestServiceA { get; set; }
public ITestServiceB TestServiceB { get; set; }
[HttpGet]
public IActionResult Get()
{
TestServiceA.Show();
TestServiceB.Show();
return Ok();
}
}
Autofac 单实例多实现
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
builder.RegisterType<TestServiceUpdate>().As<ITestServiceA>();
}
- 如果多个实现同时注册,则后注册的实现就会覆盖先注册的实现,最后将返回最后一个注册的实现。
- 如果多个实现同时注册,可以通过一个 IEnumerable<实例> 来获取到所有的实现。
private readonly IEnumerable<ITestServiceA> _testServiceAs;
public ValuesController(IEnumerable<ITestServiceA> testServiceAs)
{
_testServiceAs = testServiceAs;
}
- 当多个实现同时注册后,可以通过以下方式继续注册 实例 的所有实现。从而可以在控制器中直接使用具体实现类作为实现。
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
builder.RegisterType<TestServiceUpdate>().As<ITestServiceA>();
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource(type => type.IsAssignableTo<ITestServiceA>()));
}
private readonly TestServiceA _testServiceA;
private readonly TestServiceUpdate _testServiceUpdate;
public ValuesController(TestServiceA testServiceA,TestServiceUpdate testServiceUpdate)
{
_testServiceA = testServiceA;
_testServiceUpdate = testServiceUpdate;
}
- 扩展:Autofac 的注册逻辑可以通过 Module 来拆分管理。
public class AutofacModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
// base.Load(builder);
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
builder.RegisterType<TestServiceUpdate>().As<ITestServiceA>();
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource(type => type.IsAssignableTo<ITestServiceA>()));
}
}
public void ConfigureContainer(ContainerBuilder builder)
{
// builder.RegisterModule(new AutofacModule());
builder.RegisterModule<AutofacModule>();
}
Autofac 支持 AOP
AOP 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。Autofac 的AOP是通过 Castle(也是一个容器)项目的名为 Autofac.Extras.DynamicProxy 核心部分实现的,顾名思义其实现方式为动态代理。
安装 NuGet 程序包:
Castle.Core 4.4.1
、Autofac.Extras.DynamicProxy 6.0.0
新建自定义 AOP 拦截器
public class CustomAutofacAop : IInterceptor
{
public void Intercept(IInvocation invocation)
{
{
Console.WriteLine("方法执行前...");
}
invocation.Proceed();
{
Console.WriteLine("方法执行后...");
}
}
}
- 在接口上标记需要使用的拦截器
[Intercept(typeof(CustomAutofacAop))]
public interface ITestServiceA
{
void Show();
}
- 注册自定义拦截器,并允许实例接口使用拦截器
public void ConfigureContainer(ContainerBuilder builder)
{
//builder.RegisterType<CustomAutofacAop>();
builder.RegisterType(typeof(CustomAutofacAop));
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
builder.RegisterType<TestServiceUpdate>().As<ITestServiceA>().EnableInterfaceInterceptors();
}
- 在控制器中调用实例,即可成功执行 AOP 拦截器
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly TestServiceA _testServiceA;
private readonly TestServiceUpdate _testServiceUpdate;
public ValuesController(TestServiceA testServiceA,TestServiceUpdate testServiceUpdate)
{
_testServiceA = testServiceA;
_testServiceUpdate = testServiceUpdate;
}
public IActionResult Get()
{
_testServiceA.Show();
_testServiceUpdate.Show();
return Ok();
}
}
到此这篇关于.NET Core使用Autofac容器的DI依赖注入,IOC控制反转及AOP切面编程的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。