使用 maf重构
This commit is contained in:
@@ -31,7 +31,7 @@ public static class ServiceExtension
|
||||
|
||||
// 注册所有服务实现
|
||||
RegisterServices(services, serviceAssembly);
|
||||
|
||||
|
||||
// 注册所有仓储实现
|
||||
RegisterRepositories(services, repositoryAssembly);
|
||||
|
||||
@@ -50,20 +50,9 @@ public static class ServiceExtension
|
||||
|
||||
foreach (var @interface in interfaces)
|
||||
{
|
||||
// EmailBackgroundService 必须是 Singleton(后台服务),其他服务可用 Transient
|
||||
if (type.Name == "EmailBackgroundService")
|
||||
{
|
||||
services.AddSingleton(@interface, type);
|
||||
}
|
||||
else if (type.Name == "EmailFetchService")
|
||||
{
|
||||
// EmailFetchService 用 Transient,避免连接冲突
|
||||
services.AddTransient(@interface, type);
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddSingleton(@interface, type);
|
||||
}
|
||||
// 其他 Services 用 Singleton
|
||||
services.AddSingleton(@interface, type);
|
||||
Console.WriteLine($"✓ 注册 Service: {@interface.Name} -> {type.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,14 +65,14 @@ public static class ServiceExtension
|
||||
foreach (var type in types)
|
||||
{
|
||||
var interfaces = type.GetInterfaces()
|
||||
.Where(i => i.Name.StartsWith("I")
|
||||
.Where(i => i.Name.StartsWith("I")
|
||||
&& i.Namespace == "Repository"
|
||||
&& !i.IsGenericType); // 排除泛型接口如 IBaseRepository<T>
|
||||
|
||||
foreach (var @interface in interfaces)
|
||||
{
|
||||
services.AddSingleton(@interface, type);
|
||||
Console.WriteLine($"注册 Repository: {@interface.Name} -> {type.Name}");
|
||||
Console.WriteLine($"✓ 注册 Repository: {@interface.Name} -> {type.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user