基於autofac的屬性注入
什么是屬性注入
在了解屬性注入之前,要先了解一下DI(Dependency Injection),即依賴注入。在ASP.NET Core里自帶了一個IOC容器,而且程序支行也是基於這個容器建立起來的,在 Startup 里的 ConfigureService
方法里向容器注冊服務類型。
簡單來說,依賴注入就是容器幫我們“new”一個對象,並且管理對象的生命周期。
在依賴注入時,最常用的是構造方法注入。還有另一種方法,那就是屬性注入。
在ASP.NET Core中,自帶的容器是不支持屬性注入的,但是可以通過替換容器來實現,也就是今天介紹的:通過 Autofac 來實現屬性注入。
autofac簡介
Autofac 是一款超贊的.NET IoC 容器 . 它管理類之間的依賴關系, 從而使 應用在規模及復雜性增長的情況下依然可以輕易地修改 . 它的實現方式是將常規的.net類當做 組件 處理.
中文文檔:https://autofaccn.readthedocs.io/zh/latest/
為什么要使用屬性注入
主要有以下三點:
- 減少常用類型的重復注入代碼,使構造方法看起來更為簡潔,提高閱讀性。
- 減少或消除因構造方法注入造成子類繼承后的 base 調用鏈。
- 並非是滿足第一條或第二條就需要使用屬性注入來解決,只有當第一、二條發生的情況到達一定的數量。
具體實現
1、引用類庫
Autofac
Autofac.Extensions.DependencyInjection
2、在 Program.cs
里替換系統默認容器
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory()) // 使用 autofac 的容器工廠替換系統默認的容器
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
3、在 Startup.cs
的 ConfigureServices
里替換控制器的替換規則
public void ConfigureServices(IServiceCollection services)
{
// 替換控制器的替換規則
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
// other configure
services.AddControllers();
}
4、創建 AutowiredAttribute.cs
,用於標識使用屬性注入
[AttributeUsage(AttributeTargets.Property)]
public class AutowiredAttribute : Attribute
{
}
5、創建 AutofacModule.cs
,注冊服務
/// <summary>
/// 容器注冊類
/// </summary>
public class AutofacModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
// Register your own things directly with Autofac, like:
builder.RegisterType<HelloService>().As<IHelloService>().InstancePerDependency().AsImplementedInterfaces();
// 獲取所有控制器類型並使用屬性注入
var controllerBaseType = typeof(ControllerBase);
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
.PropertiesAutowired(new AutowiredPropertySelector());
}
}
/// <summary>
/// 屬性注入選擇器
/// </summary>
public class AutowiredPropertySelector : IPropertySelector
{
public bool InjectProperty(PropertyInfo propertyInfo, object instance)
{
// 帶有 AutowiredAttribute 特性的屬性會進行屬性注入
return propertyInfo.CustomAttributes.Any(it => it.AttributeType == typeof(AutowiredAttribute));
}
}
6、在 Startup.cs
的 方法 ConfigureContainer
里注冊上一步創建的 Module
類
// ConfigureContainer is where you can register things directly
// with Autofac. This runs after ConfigureServices so the things
// here will override registrations made in ConfigureServices.
// Don't build the container; that gets done for you. If you
// need a reference to the container, you need to use the
// "Without ConfigureContainer" mechanism shown later.
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new AutofacModule());
}
示例代碼下載:源碼
使用效果
[Autowired]
private IHelloService HelloService { get; set; }
在控制器里添加服務屬性,然后添加 [Autowired]
特性標識為屬性注入即可。
關於屬性注入的注意事項
屬性注入很好用,但是要慎重使用,因為屬性注入會造成類型的依賴關系隱藏,測試不友好等。
建議:在封閉框架時可以使用,但不能大范圍使用,只有必須使用屬性注入來達到效果的地方才會使用,用來提高使用框架時的編碼效率,來達到一些便利,脫離框架層面,編寫業務代碼時,不得使用。
參考資料
主要參考文章:
使用 autofac 實現 asp .net core 的屬性注入
ASP.NETCore 3.0 Autofac替換及控制器屬性注入及全局容器使用 - 情·深 - 博客園
autofac 的官方示例:
autofac/Examples: Example projects that consume and demonstrate Autofac IoC functionality and integration
autofac 文檔:
Welcome to Autofac’s documentation! — Autofac 5.2.0 documentation
歡迎來到 Autofac 中文文檔! — Autofac 4.0 文檔
其它:
ASP.NET Core 奇淫技巧之偽屬性注入 - 曉晨Master - 博客園
.net core2.0下Ioc容器Autofac使用 - 焰尾迭 - 博客園