AppExtensionsAddServicesFromAssemblyExceptNotUseDIs(IServiceCollection, Assembly, String, ServiceLifetime, FuncType, Boolean) Method
Namespace: QuickAdmin.CommonAssembly: QuickAdmin.Net (in QuickAdmin.Net.dll) Version: 2.0.13
public static void AddServicesFromAssemblyExceptNotUseDIs(
this IServiceCollection services,
Assembly assembly,
string typeNamePrefix = null,
ServiceLifetime defaultLifetime = ServiceLifetime.Singleton,
Func<Type, bool> funcTypeFilter = null
)
Parameters
- services IServiceCollection
- 要向其中添加服务的 IServiceCollection。
- assembly Assembly
- 程序集。
- typeNamePrefix string (Optional)
-
类型的 FullName 的前缀。比较时是区分大小写的。
可利用此参数限定命名空间,例如传入 "QHSE.Service." 表示只检索 QHSE.Service 命名空间下的类型。
- defaultLifetime ServiceLifetime (Optional)
-
要被添加的服务的生命周期的默认值。
要被添加的类若标记了 UseDIAttribute 特性,将使用该特性内设置的生命周期,否则将使用此处传入的生命周期。
- funcTypeFilter FuncType, bool (Optional)
- 用来过滤类型的函数,返回了 true 的类型才有可能被添加。
此方法用来批量注册服务:
using QuickAdmin.Common;
// 注册类型 IMovieService 所在程序集内的所有满足条件的服务
builder.Services.AddServicesFromAssemblyExceptNotUseDIs(typeof(IMovieService).Assembly);
// 注册类型 SomeType 所在程序集内的,"QHSE.Service" 命名空间下的所有满足条件的服务
builder.Services.AddServicesFromAssemblyExceptNotUseDIs(typeof(SomeType).Assembly, "QHSE.Service.");
对于每个满足指定条件的公共类:
例如调用代码为:
// 没有传入 defaultLifetime 参数值,默认为 Singleton
builder.Services.AddServicesFromAssemblyExceptNotUseDIs(typeof(MyServiceA).Assembly);
各个接口/服务的定义,以及批量注册后各服务的注册结果如下:
public interface IMyInterface1
{
}
public interface IMyInterface2
{
}
[NotUseDI]
public interface IMyInterface3
{
}
// Singleton 模式,注册类型为 IMyInterface1
public class MyServiceA : IMyInterface1
{
}
// Transient 模式,注册类型为 IMyInterface1 以及 IMyInterface2。因为没指定服务类型,所有实现的接口都会被注册
[UseDI(ServiceLifetime.Transient)]
public class MyServiceB : IMyInterface1, IMyInterface2
{
}
// Singleton 模式,注册类型为 IMyInterface2,因为指定了服务类型为 IMyInterface2
[UseDI(typeof(IMyInterface2))]
public class MyServiceC : IMyInterface1, IMyInterface2
{
}
// Singleton 模式,注册类型为 IMyInterface1。虽然也实现了多个接口,但 IMyInterface3 已标记 [NotUseDI],会被自动排除
public class MyServiceD : IMyInterface1, IMyInterface3
{
}
// 不会被注册,因为标记了 [NotUseDI]
[NotUseDI]
public class MyServiceE : IMyInterface1, IMyInterface3
{
}
// 不会被注册,因为没有实现任何接口
public class MyServiceF
{
}
// 不会被注册,虽然实现了 IMyInterface3,但该接口被标记了 [NotUseDI]
public class MyServiceG : IMyInterface3
{
}
// Singleton 模式,注册类型为 MyServiceH 自己
[UseDI(typeof(MyServiceH))]
public class MyServiceH
{
}
// 注册为 Keyed Service,Singleton 模式,注册类型为 IMyInterface1
[UseDI("someKey", ServiceLifetime.Singleton)]
public class MyServiceI : IMyInterface1
{
}
// 以下为间接实现接口示例:
public interface IMyBaseInterface1
{
}
public interface IMyBaseInterface2 : IMyBaseInterface1
{
}
public interface IMyBaseInterface3 : IMyBaseInterface2
{
}
// Singleton 模式,注册类型为 IMyBaseInterface3、IMyBaseInterface2 以及 IMyBaseInterface1。没指定服务类型,所有直接或间接实现的接口都会被注册
public class MyServiceL : IMyBaseInterface3
{
}
// Singleton 模式,注册类型为 IMyBaseInterface3
[UseDI(typeof(IMyBaseInterface3))]
public class MyServiceM : IMyBaseInterface3
{
}
// Singleton 模式,注册类型为 IMyInterface1,因 MyServiceA 实现了 IMyInterface1
public class MyServiceP : MyServiceA
{
}
如果服务直接或间接实现了多个接口,通常需要通过 [UseDI] 特性来指定要注册的是哪一个接口,除非你要把所有接口都注册。
即:若实现了多个接口但只要注册其中一个,要么对该类标记 [UseDI] 并指定该接口,要么对其它接口标记 [NotUseDI]。