fix
This commit is contained in:
36
Application/Extensions/ServiceCollectionExtensions.cs
Normal file
36
Application/Extensions/ServiceCollectionExtensions.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace Application.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Application层服务注册扩展
|
||||
/// </summary>
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册所有Application层服务
|
||||
/// </summary>
|
||||
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
|
||||
{
|
||||
// 获取Application层程序集
|
||||
var assembly = typeof(ServiceCollectionExtensions).Assembly;
|
||||
|
||||
// 自动注册所有以"Application"结尾的类
|
||||
// 匹配接口规则: IXxxApplication -> XxxApplication
|
||||
var applicationTypes = assembly.GetTypes()
|
||||
.Where(t => t.IsClass && !t.IsAbstract && t.Name.EndsWith("Application"))
|
||||
.ToList();
|
||||
|
||||
foreach (var implementationType in applicationTypes)
|
||||
{
|
||||
// 查找对应的接口 IXxxApplication
|
||||
var interfaceType = implementationType.GetInterfaces()
|
||||
.FirstOrDefault(i => i.Name == $"I{implementationType.Name}");
|
||||
|
||||
if (interfaceType != null)
|
||||
{
|
||||
services.AddScoped(interfaceType, implementationType);
|
||||
}
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user