给微软的依赖注入框架写一些扩展方法

给微软的依赖注入框架写一些扩展方法

Intro

现在在项目里大多都是直接使用微软的依赖注入框架,而微软的注入方式比较简单,不如 AutoFac 使用起来灵活,于是想给微软的依赖注入增加一些扩展,使得可以像AutoFac 一样比较灵活的注册服务

Extensions

RegisterTypeAsImplementedInterface

将类型注册为其实现的接口,比如 pubic class UserService:IUserService,IUserRepository{}

注册 UserServiceIUserServiceIUserRepository

等效于:

services.AddSingleton();
services.AddSingleton();

实现代码:

/// 
/// RegisterTypeAsImplementedInterfaces
/// 
/// services
/// type
/// service lifetime
/// services
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 来过滤要注册的类型

使用示例:

services.RegisterAssemblyTypesAsImplementedInterface(t => !t.IsAbstract && t.IsClass && t.IsAssignableTo(), typeof(NoticeViewEventHandler).Assembly);

实现源码:


/// 
/// RegisterTypeAsImplementedInterfaces
/// 
/// services
/// assemblies
/// services
public static IServiceCollection RegisterAssemblyTypesAsImplementedInterfaces(this IServiceCollection services,
    params Assembly[] assemblies)
    => RegisterAssemblyTypesAsImplementedInterfaces(services, typesFilter: null, ServiceLifetime.Singleton, assemblies);

/// 
/// RegisterTypeAsImplementedInterfaces
/// 
/// services
/// service lifetime
/// assemblies
/// services
public static IServiceCollection RegisterAssemblyTypesAsImplementedInterfaces(this IServiceCollection services,
    ServiceLifetime serviceLifetime, params Assembly[] assemblies)
    => RegisterAssemblyTypesAsImplementedInterfaces(services, typesFilter: null, serviceLifetime, assemblies);

/// 
/// RegisterTypeAsImplementedInterfaces, singleton by default
/// 
/// services
/// filter types to register
/// assemblies
/// services
public static IServiceCollection RegisterAssemblyTypesAsImplementedInterfaces(this IServiceCollection services, Func typesFilter, params Assembly[] assemblies)
    => RegisterAssemblyTypesAsImplementedInterfaces(services, typesFilter: typesFilter, ServiceLifetime.Singleton, assemblies);

/// 
/// RegisterTypeAsImplementedInterfaces
/// 
/// services
/// filter types to register
/// service lifetime
/// assemblies
/// services
public static IServiceCollection RegisterAssemblyTypesAsImplementedInterfaces(this IServiceCollection services, Func 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

注册类型,这相当于 AutoFacRegisterAssemblyTypes().AsSelf()

services.RegisterAssemblyTypes(t => !t.IsAbstract && t.IsClass && t.IsAssignableTo(), typeof(NoticeViewEventHandler).Assembly);

实现源码:

/// 
/// RegisterAssemblyTypes
/// 
/// services
/// assemblies
/// services
public static IServiceCollection RegisterAssemblyTypes(this IServiceCollection services, params Assembly[] assemblies)
    => RegisterAssemblyTypes(services, null, ServiceLifetime.Singleton, assemblies);

/// 
/// RegisterAssemblyTypes
/// 
/// services
/// service lifetime
/// assemblies
/// services
public static IServiceCollection RegisterAssemblyTypes(this IServiceCollection services,
    ServiceLifetime serviceLifetime, params Assembly[] assemblies)
    => RegisterAssemblyTypes(services, null, serviceLifetime, assemblies);

/// 
/// RegisterAssemblyTypes
/// 
/// services
/// filter types to register
/// assemblies
/// services
public static IServiceCollection RegisterAssemblyTypes(this IServiceCollection services,
    Func typesFilter, params Assembly[] assemblies)
    => RegisterAssemblyTypes(services, typesFilter, ServiceLifetime.Singleton, assemblies);

/// 
/// RegisterAssemblyTypes
/// 
/// services
/// filter types to register
/// service lifetime
/// assemblies
/// services
public static IServiceCollection RegisterAssemblyTypes(this IServiceCollection services, Func 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) 的方法用来注册服务

public interface IServiceModule
{
    void ConfigureServices(IServiceCollection services);
}

ServiceModule Demo

public class BusinessServiceModule : IServiceModule
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddEFRepository();
        services.AddBLL();
        services.TryAddScoped();
        services.TryAddSingleton();
        services.TryAddSingleton();
    }
}

RegisterAssemblyModules

上面我们定义了一个 Module,下面来定义一个扩展方法来注册 module 中的服务信息

使用示例:

// load service modules
services.RegisterAssemblyModules(pluginAssemblies);

实现源码:

/// 
/// RegisterAssemblyModules
/// 
/// services
/// assemblies
/// services
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

你可能感兴趣的:(给微软的依赖注入框架写一些扩展方法)