IT教程 ·

给微软的依靠注入框架写一些扩大要领

【自制操作系统10】内存管理系统

给微软的依靠注入框架写一些扩大要领

Intro

现在在项目里大多都是直接运用微软的依靠注入框架,而微软的注入体式格局比较简单,不如 AutoFac 运用起来天真,因而想给微软的依靠注入增添一些扩大,使得能够像AutoFac 一样比较天真的注册效劳

Extensions

RegisterTypeAsImplementedInterface

将范例注册为其完成的接口,比方 pubic class UserService:IUserService,IUserRepository{}

注册 UserServiceIUserServiceIUserRepository

等效于:

services.AddSingleton<IUserService, UserService>();
services.AddSingleton<IUserRepository, UserService>();

完成代码:

/// <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 来过滤要注册的范例

运用示例:

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

完成源码:


/// <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

注册范例,这相当于 AutoFacRegisterAssemblyTypes().AsSelf()

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

完成源码:

/// <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) 的要领用来注册效劳

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

ServiceModule Demo

public class BusinessServiceModule : IServiceModule
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddEFRepository();
        services.AddBLL();
        services.TryAddScoped<ReservationHelper>();
        services.TryAddSingleton<CaptchaVerifyHelper>();
        services.TryAddSingleton<OperLogHelper>();
    }
}

RegisterAssemblyModules

上面我们定义了一个 Module,下面来定义一个扩大要领来注册 module 中的效劳信息

运用示例:

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

完成源码:

/// <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

【区块链】关于ETH/BTC区块的监控

参与评论