All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 4m27s
Docker Build & Deploy / Deploy to Production (push) Successful in 7s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
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);
|
|
}
|
|
else
|
|
{
|
|
// 如果没有接口,直接注册实现类
|
|
services.AddScoped(implementationType);
|
|
}
|
|
}
|
|
|
|
return services;
|
|
}
|
|
}
|