.NET?Core使用Autofac容器的DI依賴注入,IOC控制反轉(zhuǎn)及AOP切面編程
Autofac 容器
Autofac 是一款.NET IoC 容器 . 它管理類之間的依賴關(guān)系, 從而使 應(yīng)用在規(guī)模及復(fù)雜性增長(zhǎng)的情況下依然可以輕易地修改 . 它的實(shí)現(xiàn)方式是將常規(guī)的.net類當(dāng)做組件處理.
- 安裝 NuGet 程序包:
Autofac 6.0.0 - 創(chuàng)建一個(gè) ContainerBuiler
- 注冊(cè)接口和實(shí)現(xiàn)關(guān)系
- 通過 ContainerBuiler 的 Build 方法,得到 IContainer 容器
- 通過 IContainer 容器獲取實(shí)例
- 使用服務(wù)
ITestServiceA和TestServiceA
public interface ITestServiceA
{
void Show();
}
public class TestServiceA : ITestServiceA
{
public TestServiceA()
{
Console.WriteLine($"{this.GetType().Name} 被構(gòu)造了...");
}
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(); // 獲取服務(wù)實(shí)例 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} 被構(gòu)造了...");
}
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} 被構(gòu)造了...");
}
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} 被構(gòu)造了...");
}
public void Show()
{
// TestServiceA.Show();
// TestServiceB.Show();
// TestServiceC.Show();
Console.WriteLine($"This is a {this.GetType().Name} Instance...");
}
}- 構(gòu)造函數(shù)注入
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(); // 獲取服務(wù)實(shí)例 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(); // 獲取服務(wù)實(shí)例 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();
// 獲取服務(wù)實(shí)例
var testService = container.Resolve<ITestServiceB>();
testService.Show();Autofac 生命周期
- InstancePerDependency :默認(rèn)模式,每次調(diào)用,都會(huì)重新實(shí)例化對(duì)象;每次請(qǐng)求都創(chuàng)建一個(gè)新的對(duì)象;
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 :?jiǎn)卫J?,每次調(diào)用,都會(huì)使用同一個(gè)實(shí)例化的對(duì)象;每次都用同一個(gè)對(duì)象;
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 : 同一個(gè)生命周期域中,每次調(diào)用,都會(huì)使用同一個(gè)實(shí)例化的對(duì)象;每次都用同一個(gè)對(duì)象;且每個(gè)不同的生命周期域中的實(shí)例是唯一的,不共享的。
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 : 同一個(gè)匹配的生命周期域中,每次調(diào)用,都會(huì)使用同一個(gè)實(shí)例化的對(duì)象;每次都用同一個(gè)對(duì)象;且每個(gè)不匹配的生命周期域中的實(shí)例是唯一的,不共享的。
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 : 在一個(gè)所擁有的實(shí)例創(chuàng)建的生命周期中,每次調(diào)用,都會(huì)使用同一個(gè)實(shí)例化的對(duì)象;每次都用同一個(gè)對(duì)象;(較少使用)
InstancePerHttpRequest : 同一次Http請(qǐng)求上下文中,每次調(diào)用,都會(huì)使用同一個(gè)實(shí)例化的對(duì)象;每次都用同一個(gè)對(duì)象;僅適用于 ASP.NET (CORE) MVC 或 WebForm 應(yīng)用程序
Autofac 支持配置文件
- 安裝 NuGet 程序包:
Autofac.Extensions.DependencyInjection 7.1.0、Autofac.Configuration 6.0.0 - 新建配置文件(指定接口和實(shí)現(xiàn)的對(duì)應(yīng)關(guān)系)
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
}
]
}- 讀取配置文件,完成服務(wù)對(duì)應(yīng)關(guān)系的注冊(cè)
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();
// 獲取服務(wù)實(shí)例
var testServiceA = container.Resolve<ITestServiceA>();
var testServiceD = container.Resolve<ITestServiceD>();
testServiceD.Show();- 新建 ITestServiceA 的新版實(shí)現(xiàn)類
TestServiceUpdate
public class TestServiceUpdate : ITestServiceA
{
public TestServiceUpdate()
{
Console.WriteLine($"{this.GetType().Name} 被構(gòu)造了...");
}
public void Show()
{
Console.WriteLine($"This is a {this.GetType().Name} Instance...");
}
}- 通過修改配置文件
autofac.json來實(shí)現(xiàn)快速實(shí)現(xiàn) ITestServiceA 的實(shí)現(xiàn)的重新定義:
{
"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.0Program文件中指定 Autofac 工廠替換默認(rèn)工廠:
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>();
}- 通過控制器構(gòu)造函數(shù)注入,獲取實(shí)例
[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 注冊(cè)的服務(wù),將來也會(huì)交給 Autofac 管理
public void ConfigureServices(IServiceCollection services)
{
#region IServiceCollection 注冊(cè)的服務(wù),將來也會(huì)交給 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 支持控制器屬性注入
控制器本身是一個(gè)類,它的實(shí)例其實(shí)是有 IControllerActivator 來創(chuàng)建的。
- 指定控制器的實(shí)例由容器來創(chuàng)建
public void ConfigureServices(IServiceCollection services)
{
// ...
#region 指定控制器的實(shí)例由容器來創(chuàng)建
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
#endregion
}- 注冊(cè)控制器的抽象和具體的關(guān)系
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<TestServiceA>().As<ITestServiceA>().PropertiesAutowired();
builder.RegisterType<TestServiceB>().As<ITestServiceB>();
#region 注冊(cè)所有控制器的關(guān)系及控制器實(shí)例化所需要的組件
var controllersTypesInAssembly = typeof(Startup).Assembly.GetExportedTypes()
.Where(type => typeof(ControllerBase).IsAssignableFrom(type)).ToArray();
builder.RegisterTypes(controllersTypesInAssembly)
.PropertiesAutowired();
#endregion
}- 在控制器內(nèi)定義屬性
[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();
}
}- 擴(kuò)展:自己控制哪些屬性需要做依賴注入(默認(rèn)是讓控制器中的屬性都依賴注入)
[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 注冊(cè)所有控制器的關(guān)系及控制器實(shí)例化所需要的組件
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 單實(shí)例多實(shí)現(xiàn)
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
builder.RegisterType<TestServiceUpdate>().As<ITestServiceA>();
}- 如果多個(gè)實(shí)現(xiàn)同時(shí)注冊(cè),則后注冊(cè)的實(shí)現(xiàn)就會(huì)覆蓋先注冊(cè)的實(shí)現(xiàn),最后將返回最后一個(gè)注冊(cè)的實(shí)現(xiàn)。
- 如果多個(gè)實(shí)現(xiàn)同時(shí)注冊(cè),可以通過一個(gè) IEnumerable<實(shí)例> 來獲取到所有的實(shí)現(xiàn)。
private readonly IEnumerable<ITestServiceA> _testServiceAs;
public ValuesController(IEnumerable<ITestServiceA> testServiceAs)
{
_testServiceAs = testServiceAs;
}- 當(dāng)多個(gè)實(shí)現(xiàn)同時(shí)注冊(cè)后,可以通過以下方式繼續(xù)注冊(cè) 實(shí)例 的所有實(shí)現(xiàn)。從而可以在控制器中直接使用具體實(shí)現(xiàn)類作為實(shí)現(xiàn)。
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;
}- 擴(kuò)展:Autofac 的注冊(cè)邏輯可以通過 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 面向切面編程,通過預(yù)編譯方式和運(yùn)行期動(dòng)態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。Autofac 的AOP是通過 Castle(也是一個(gè)容器)項(xiàng)目的名為 Autofac.Extras.DynamicProxy 核心部分實(shí)現(xiàn)的,顧名思義其實(shí)現(xiàn)方式為動(dòng)態(tài)代理。
安裝 NuGet 程序包:
Castle.Core 4.4.1、Autofac.Extras.DynamicProxy 6.0.0新建自定義 AOP 攔截器
public class CustomAutofacAop : IInterceptor
{
public void Intercept(IInvocation invocation)
{
{
Console.WriteLine("方法執(zhí)行前...");
}
invocation.Proceed();
{
Console.WriteLine("方法執(zhí)行后...");
}
}
}- 在接口上標(biāo)記需要使用的攔截器
[Intercept(typeof(CustomAutofacAop))]
public interface ITestServiceA
{
void Show();
}- 注冊(cè)自定義攔截器,并允許實(shí)例接口使用攔截器
public void ConfigureContainer(ContainerBuilder builder)
{
//builder.RegisterType<CustomAutofacAop>();
builder.RegisterType(typeof(CustomAutofacAop));
builder.RegisterType<TestServiceA>().As<ITestServiceA>();
builder.RegisterType<TestServiceUpdate>().As<ITestServiceA>().EnableInterfaceInterceptors();
}- 在控制器中調(diào)用實(shí)例,即可成功執(zhí)行 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();
}
}到此這篇關(guān)于.NET Core使用Autofac容器的DI依賴注入,IOC控制反轉(zhuǎn)及AOP切面編程的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Asp.Net Core 中的“虛擬目錄”實(shí)現(xiàn)
這篇文章主要介紹了Asp.Net Core 中的“虛擬目錄”實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
asp.net AutoCompleteExtender的一個(gè)簡(jiǎn)單例子代碼
asp.net AutoCompleteExtender的一個(gè)簡(jiǎn)單例子代碼2009-12-12
asp.net下用js實(shí)現(xiàn)鼠標(biāo)移至小圖,自動(dòng)顯示相應(yīng)大圖
asp.net下用js實(shí)現(xiàn)鼠標(biāo)移至小圖,自動(dòng)顯示相應(yīng)大圖...2007-03-03
如何利用HttpClientFactory實(shí)現(xiàn)簡(jiǎn)單的熔斷降級(jí)
這篇文章主要給大家介紹了關(guān)于如何利用HttpClientFactory實(shí)現(xiàn)簡(jiǎn)單的熔斷降級(jí)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
關(guān)于dotnet?替換?ASP.NET?Core?的底層通訊為命名管道的?IPC?庫的問題
這篇文章主要介紹了dotnet?替換?ASP.NET?Core?的底層通訊為命名管道的?IPC?庫,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
ASP.NET GridView中加入RadioButton不能單選的解決方案
這篇文章主要介紹了ASP.NET GridView中加入RadioButton不能單選的解決方案,希望大家閱讀完本文有所收獲。2015-09-09
asp.net 學(xué)習(xí)之路 項(xiàng)目整體框架簡(jiǎn)單的搭建
最近剛學(xué)了些關(guān)于asp.net mvc方面的知識(shí),于是了要拿個(gè)小項(xiàng)目來練練手,提高下自己的code能力跟思維能力2012-12-12

