给微软的依赖注入框架写一些扩展方法
Intro#
现在在项目里大多都是直接使用微软的依赖注入框架,而微软的注入方式比较简单,不如 AutoFac 使用起来灵活,于是想给微软的依赖注入增加一些扩展,使得可以像AutoFac 一样比较灵活的注册服务
Extensions#
RegisterTypeAsImplementedInterface#
将类型注册为其实现的接口,比如 pubic class UserService:IUserService,IUserRepository{}
,
注册 UserService
为 IUserService
和 IUserRepository
等效于:
Copyservices.AddSingleton<IUserService, UserService>();
services.AddSingleton<IUserRepository, UserService>();
实现代码:
Copy/// <summary>
/// RegisterTypeAsImplementedInterfaces
/// </summary>
/// <param name="services">services</param>
/// <param name="type">type</param>
/// <param name="serviceLifetime">service lifetime</param>
/// <returns>services</returns>
public static IServiceCollection RegisterTypeAsImplementedInterfaces(this IServiceCollection services, Type type, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
{
if (type != null)
{
foreach (var interfaceType in type.GetImplementedInterfaces())
{
services.Add(new ServiceDescriptor(interfaceType, type, serviceLifetime));
}
}
return services;
}
RegisterAssemblyTypesAsImplementedInterface#
在上面的基础上,加载指定程序集中的类型(不指定程序集则使用当前应用程序域中所有程序集),可以通过 typesFilter
来过滤要注册的类型
使用示例:
Copyservices.RegisterAssemblyTypesAsImplementedInterface(t => !t.IsAbstract && t.IsClass && t.IsAssignableTo<IEventHandler>(), typeof(NoticeViewEventHandler).Assembly);
实现源码:
Copy
/// <summary>
/// RegisterTypeAsImplementedInterfaces
/// </summary>
/// <param name="services">services</param>
/// <param name="assemblies">assemblies</param>
/// <returns>services</returns>
public static IServiceCollection RegisterAssemblyTypesAsImplementedInterfaces(this IServiceCollection services,
params Assembly[] assemblies)
=> RegisterAssemblyTypesAsImplementedInterfaces(services, typesFilter: null, ServiceLifetime.Singleton, assemblies);
/// <summary>
/// RegisterTypeAsImplementedInterfaces
/// </summary>
/// <param name="services">services</param>
/// <param name="serviceLifetime">service lifetime</param>
/// <param name="assemblies">assemblies</param>
/// <returns>services</returns>
public static IServiceCollection RegisterAssemblyTypesAsImplementedInterfaces(this IServiceCollection services,
ServiceLifetime serviceLifetime, params Assembly[] assemblies)
=> RegisterAssemblyTypesAsImplementedInterfaces(services, typesFilter: null, serviceLifetime, assemblies);
/// <summary>
/// RegisterTypeAsImplementedInterfaces, singleton by default
/// </summary>
/// <param name="services">services</param>
/// <param name="typesFilter">filter types to register</param>
/// <param name="assemblies">assemblies</param>
/// <returns>services</returns>
public static IServiceCollection RegisterAssemblyTypesAsImplementedInterfaces(this IServiceCollection services, Func<Type, bool> typesFilter, params Assembly[] assemblies)
=> RegisterAssemblyTypesAsImplementedInterfaces(services, typesFilter: typesFilter, ServiceLifetime.Singleton, assemblies);
/// <summary>
/// RegisterTypeAsImplementedInterfaces
/// </summary>
/// <param name="services">services</param>
/// <param name="typesFilter">filter types to register</param>
/// <param name="serviceLifetime">service lifetime</param>
/// <param name="assemblies">assemblies</param>
/// <returns>services</returns>
public static IServiceCollection RegisterAssemblyTypesAsImplementedInterfaces(this IServiceCollection services, Func<Type, bool> typesFilter, ServiceLifetime serviceLifetime, params Assembly[] assemblies)
{
if (assemblies == null || assemblies.Length == 0)
{
assemblies = ReflectHelper.GetAssemblies();
}
var types = assemblies
.Select(assembly => assembly.GetExportedTypes())
.SelectMany(t => t);
if (typesFilter != null)
{
types = types.Where(typesFilter);
}
foreach (var type in types)
{
foreach (var implementedInterface in type.GetImplementedInterfaces())
{
services.Add(new ServiceDescriptor(implementedInterface, type, serviceLifetime));
}
}
return services;
}
RegisterAssemblyTypes#
注册类型,这相当于 AutoFac
中 RegisterAssemblyTypes().AsSelf()
Copyservices.RegisterAssemblyTypes(t => !t.IsAbstract && t.IsClass && t.IsAssignableTo<IEventHandler>(), typeof(NoticeViewEventHandler).Assembly);
实现源码:
Copy/// <summary>
/// RegisterAssemblyTypes
/// </summary>
/// <param name="services">services</param>
/// <param name="assemblies">assemblies</param>
/// <returns>services</returns>
public static IServiceCollection RegisterAssemblyTypes(this IServiceCollection services, params Assembly[] assemblies)
=> RegisterAssemblyTypes(services, null, ServiceLifetime.Singleton, assemblies);
/// <summary>
/// RegisterAssemblyTypes
/// </summary>
/// <param name="services">services</param>
/// <param name="serviceLifetime">service lifetime</param>
/// <param name="assemblies">assemblies</param>
/// <returns>services</returns>
public static IServiceCollection RegisterAssemblyTypes(this IServiceCollection services,
ServiceLifetime serviceLifetime, params Assembly[] assemblies)
=> RegisterAssemblyTypes(services, null, serviceLifetime, assemblies);
/// <summary>
/// RegisterAssemblyTypes
/// </summary>
/// <param name="services">services</param>
/// <param name="typesFilter">filter types to register</param>
/// <param name="assemblies">assemblies</param>
/// <returns>services</returns>
public static IServiceCollection RegisterAssemblyTypes(this IServiceCollection services,
Func<Type, bool> typesFilter, params Assembly[] assemblies)
=> RegisterAssemblyTypes(services, typesFilter, ServiceLifetime.Singleton, assemblies);
/// <summary>
/// RegisterAssemblyTypes
/// </summary>
/// <param name="services">services</param>
/// <param name="typesFilter">filter types to register</param>
/// <param name="serviceLifetime">service lifetime</param>
/// <param name="assemblies">assemblies</param>
/// <returns>services</returns>
public static IServiceCollection RegisterAssemblyTypes(this IServiceCollection services, Func<Type, bool> typesFilter, ServiceLifetime serviceLifetime, params Assembly[] assemblies)
{
if (assemblies == null || assemblies.Length == 0)
{
assemblies = ReflectHelper.GetAssemblies();
}
var types = assemblies
.Select(assembly => assembly.GetExportedTypes())
.SelectMany(t => t);
if (typesFilter != null)
{
types = types.Where(typesFilter);
}
foreach (var type in types)
{
services.Add(new ServiceDescriptor(type, type, serviceLifetime));
}
return services;
}
ServiceModule#
在 AutoFac 中有一个 Module 的概念可以让我们更方便的注册,一般可以在一个 Module 里注册需要服务注册的服务,这样注册的好处在于容易做插件化,新的插件需要注册什么样的服务,外界并不知道,如果用 Module 的注册方式,我只需要调用插件中的 Module 就可以注册所需要的服务了
IServiceModule#
类似于 AutoFac 中的 Module,我们也可以定义一个 IServiceModule
,定义一个 ConfigueServices(IServiceCollection services)
的方法用来注册服务
Copypublic interface IServiceModule
{
void ConfigureServices(IServiceCollection services);
}
ServiceModule Demo#
Copypublic class BusinessServiceModule : IServiceModule
{
public void ConfigureServices(IServiceCollection services)
{
services.AddEFRepository();
services.AddBLL();
services.TryAddScoped<ReservationHelper>();
services.TryAddSingleton<CaptchaVerifyHelper>();
services.TryAddSingleton<OperLogHelper>();
}
}
RegisterAssemblyModules#
上面我们定义了一个 Module,下面来定义一个扩展方法来注册 module 中的服务信息
使用示例:
Copy// load service modules
services.RegisterAssemblyModules(pluginAssemblies);
实现源码:
Copy/// <summary>
/// RegisterAssemblyModules
/// </summary>
/// <param name="services">services</param>
/// <param name="assemblies">assemblies</param>
/// <returns>services</returns>
public static IServiceCollection RegisterAssemblyModules(
[NotNull] this IServiceCollection services, params Assembly[] assemblies)
{
if (assemblies == null || assemblies.Length == 0)
{
assemblies = ReflectHelper.GetAssemblies();
}
foreach (var type in assemblies.SelectMany(ass => ass.GetTypes())
.Where(t => t.IsClass && !t.IsAbstract && typeof(IServiceModule).IsAssignableFrom(t))
)
{
try
{
if (Activator.CreateInstance(type) is IServiceModule module)
{
module.ConfigureServices(services);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
return services;
}
Reference#
- https://github.com/WeihanLi/WeihanLi.Common/blob/58cec5fe93baa3ad73df6e339e2fcb609a7b4b79/src/WeihanLi.Common/Extensions/ServiceCollectionExtension.cs
- https://github.com/WeihanLi/ActivityReservation/blob/dev/ActivityReservation/Startup.cs
- https://github.com/WeihanLi/ActivityReservation/blob/dev/ActivityReservation.Helper/Services/BusinessServiceModule.cs
- https://github.com/WeihanLi/DbTool/blob/wpf-dev/DbTool/App.xaml.cs
- https://autofac.readthedocs.io/en/latest/configuration/modules.html