精準(zhǔn)掌握.NET依賴注入:DI自動注冊服務(wù)輕松搞定
概述:.NET依賴注入(DI)通過反射自動注冊服務(wù),示例展示了注冊指定類、帶特性類、項(xiàng)目下所有接口實(shí)現(xiàn)的類。簡化配置,提高可維護(hù)性。
在.NET中,進(jìn)行依賴注入(DI)的自動注冊,可以通過反射機(jī)制和程序集掃描來實(shí)現(xiàn)。以下是詳細(xì)的步驟以及相應(yīng)的C#源代碼示例,包括注冊指定類、注冊帶有自定義特性的類、以及注冊項(xiàng)目下所有帶有接口實(shí)現(xiàn)的類(項(xiàng)目下的所有接口):
步驟1:創(chuàng)建接口和實(shí)現(xiàn)類
// 接口1
public interface IService1
{
void PerformService1();
}
// 接口2
public interface IService2
{
void PerformService2();
}
// 實(shí)現(xiàn)類1,實(shí)現(xiàn)IService1
public class MyService1 : IService1
{
public void PerformService1()
{
Console.WriteLine("Service 1 performed.");
}
}
// 實(shí)現(xiàn)類2,實(shí)現(xiàn)IService2
[CustomRegistration] // 帶有自定義特性
public class MyService2 : IService2
{
public void PerformService2()
{
Console.WriteLine("Service 2 performed.");
}
}
// 實(shí)現(xiàn)類3,實(shí)現(xiàn)IService1和IService2
public class MyService3 : IService1, IService2
{
public void PerformService1()
{
Console.WriteLine("Service 3 (Service 1 part) performed.");
}
public void PerformService2()
{
Console.WriteLine("Service 3 (Service 2 part) performed.");
}
}
步驟2:創(chuàng)建自定義特性
// 自定義特性
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
sealed class CustomRegistrationAttribute : Attribute
{
}
步驟3:創(chuàng)建自動注冊方法
using System;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
class Program
{
static void Main()
{
// 創(chuàng)建服務(wù)集合
var services = new ServiceCollection();
// 步驟4:注冊指定類
services.AddTransient<MyService1>();
// 步驟5:注冊帶有自定義特性的類
RegisterClassesWithAttribute<CustomRegistrationAttribute>(services);
// 步驟6:注冊項(xiàng)目下所有帶有接口實(shí)現(xiàn)的類(項(xiàng)目下的所有接口)
RegisterAllImplementationsOfInterfaces(services);
// 構(gòu)建服務(wù)提供程序
var serviceProvider = services.BuildServiceProvider();
// 步驟7:使用注冊的服務(wù)
var myService1 = serviceProvider.GetService<MyService1>();
myService1.PerformService1();
var myService2 = serviceProvider.GetService<MyService2>();
myService2.PerformService2();
var myService3 = serviceProvider.GetService<MyService3>();
myService3.PerformService1();
myService3.PerformService2();
}
// 自動注冊帶有指定特性的類
static void RegisterClassesWithAttribute<TAttribute>(IServiceCollection services)
where TAttribute : Attribute
{
// 獲取當(dāng)前程序集
var assembly = Assembly.GetExecutingAssembly();
// 獲取帶有指定特性的所有類
var attributedTypes = assembly.GetTypes()
.Where(type => type.GetCustomAttributes(typeof(TAttribute), true).Any() && type.IsClass);
// 注冊這些類
foreach (var attributedType in attributedTypes)
{
services.AddTransient(attributedType);
}
}
// 自動注冊項(xiàng)目下所有帶有接口實(shí)現(xiàn)的類(項(xiàng)目下的所有接口)
static void RegisterAllImplementationsOfInterfaces(IServiceCollection services)
{
// 獲取當(dāng)前程序集
var assembly = Assembly.GetExecutingAssembly();
// 獲取項(xiàng)目下所有接口
var interfaceTypes = assembly.GetTypes()
.Where(type => type.IsInterface);
// 獲取實(shí)現(xiàn)了這些接口的所有類
var implementationTypes = assembly.GetTypes()
.Where(type => interfaceTypes.Any(interfaceType => interfaceType.IsAssignableFrom(type)) && type.IsClass);
// 注冊這些類
foreach (var implementationType in implementationTypes)
{
services.AddTransient(implementationType);
}
}
}
在上述代碼中:
- 使用AddTransient方法注冊了特定的MyService1類。
- 使用RegisterClassesWithAttribute方法注冊了帶有CustomRegistrationAttribute特性的類。這里使用了反射機(jī)制來動態(tài)獲取所有帶有指定特性的類的類型,并將它們注冊到DI容器中。
- 使用RegisterAllImplementationsOfInterfaces方法注冊了項(xiàng)目下所有實(shí)現(xiàn)接口的類。
請確保在項(xiàng)目中引用了Microsoft.Extensions.DependencyInjection相關(guān)的包。這是一個基本的示例,實(shí)際應(yīng)用中可能需要更復(fù)雜的配置,具體取決于項(xiàng)目的需求。