精準掌握.NET依賴注入:DI自動注冊服務輕松搞定
作者:架構師老盧
在.NET中,進行依賴注入(DI)的自動注冊,可以通過反射機制和程序集掃描來實現(xiàn)。以下是詳細的步驟以及相應的C#源代碼示例,包括注冊指定類、注冊帶有自定義特性的類、以及注冊項目下所有帶有接口實現(xiàn)的類(項目下的所有接口)。
概述:.NET依賴注入(DI)通過反射自動注冊服務,示例展示了注冊指定類、帶特性類、項目下所有接口實現(xiàn)的類。簡化配置,提高可維護性。
在.NET中,進行依賴注入(DI)的自動注冊,可以通過反射機制和程序集掃描來實現(xiàn)。以下是詳細的步驟以及相應的C#源代碼示例,包括注冊指定類、注冊帶有自定義特性的類、以及注冊項目下所有帶有接口實現(xiàn)的類(項目下的所有接口):
步驟1:創(chuàng)建接口和實現(xiàn)類
// 接口1
public interface IService1
{
void PerformService1();
}
// 接口2
public interface IService2
{
void PerformService2();
}
// 實現(xiàn)類1,實現(xiàn)IService1
public class MyService1 : IService1
{
public void PerformService1()
{
Console.WriteLine("Service 1 performed.");
}
}
// 實現(xiàn)類2,實現(xiàn)IService2
[CustomRegistration] // 帶有自定義特性
public class MyService2 : IService2
{
public void PerformService2()
{
Console.WriteLine("Service 2 performed.");
}
}
// 實現(xiàn)類3,實現(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)建服務集合
var services = new ServiceCollection();
// 步驟4:注冊指定類
services.AddTransient<MyService1>();
// 步驟5:注冊帶有自定義特性的類
RegisterClassesWithAttribute<CustomRegistrationAttribute>(services);
// 步驟6:注冊項目下所有帶有接口實現(xiàn)的類(項目下的所有接口)
RegisterAllImplementationsOfInterfaces(services);
// 構建服務提供程序
var serviceProvider = services.BuildServiceProvider();
// 步驟7:使用注冊的服務
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
{
// 獲取當前程序集
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àn)的類(項目下的所有接口)
static void RegisterAllImplementationsOfInterfaces(IServiceCollection services)
{
// 獲取當前程序集
var assembly = Assembly.GetExecutingAssembly();
// 獲取項目下所有接口
var interfaceTypes = assembly.GetTypes()
.Where(type => type.IsInterface);
// 獲取實現(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特性的類。這里使用了反射機制來動態(tài)獲取所有帶有指定特性的類的類型,并將它們注冊到DI容器中。
- 使用RegisterAllImplementationsOfInterfaces方法注冊了項目下所有實現(xiàn)接口的類。
請確保在項目中引用了Microsoft.Extensions.DependencyInjection相關的包。這是一個基本的示例,實際應用中可能需要更復雜的配置,具體取決于項目的需求。
責任編輯:姜華
來源:
今日頭條