141 lines
5.4 KiB
C#
141 lines
5.4 KiB
C#
using FluentValidation;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using PARR.Core.Common.Implementations;
|
|
using PARR.Core.Common.Interfaces;
|
|
using PARR.Core.Services.Task.Handlers;
|
|
using PARR.Core.Services.Task.Handlers.Factory;
|
|
using PARR.Core.Services.Task.Implementations;
|
|
using PARR.Core.Services.Task.Interfaces;
|
|
using PARR.Core.Services.Task.Providers;
|
|
using PARR.Core.Services.Workload.Implementations;
|
|
using PARR.Core.Services.Workload.Interfaces;
|
|
using PARR.Domain.Enums;
|
|
using PARR.Domain.Settings;
|
|
|
|
namespace PARR.Core
|
|
{
|
|
public static class DependencyInjection
|
|
{
|
|
|
|
/// <summary>
|
|
/// Регистрация Core (BLL) сервисов
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <returns></returns>
|
|
public static IServiceCollection AddCoreServices(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
// Регим профили AutoMapper
|
|
services.AddBllMapping();
|
|
|
|
// Регим все валидаторы в Core
|
|
services.AddValidatorsFromAssembly(typeof(DependencyInjection).Assembly, includeInternalTypes: true);
|
|
|
|
// Регим настройки
|
|
services.RegisterSettings(configuration);
|
|
|
|
// --- Регистрируем сревисы Core ---
|
|
|
|
#region Common
|
|
|
|
services.AddTransient<ITransformService, TransformService>();
|
|
|
|
#endregion
|
|
|
|
#region Task
|
|
|
|
// ---------> Регистрируем в AddTaskManagement <---------
|
|
|
|
//// Регистрация хендлеров (нужно регистировать каждый отдельно)
|
|
//services.AddScoped<WorkloadReportHandler>();
|
|
//services.AddScoped<BaseTaskHandler>(sp => sp.GetRequiredService<WorkloadReportHandler>());// регим для фабрики
|
|
//// Еще хэндлеры...
|
|
|
|
//// Фабрика хэндлеров
|
|
//services.AddScoped<ITaskHandlerFactory, TaskHandlerFactory>();
|
|
|
|
//services.AddScoped<ITaskManagementService, TaskManagementService>();
|
|
|
|
//// Провайдер настроек MQ
|
|
//services.AddSingleton<ITaskMqSettingsProvider, TaskMqSettingsProvider>();
|
|
|
|
#endregion
|
|
|
|
#region Services
|
|
|
|
|
|
//services.AddScoped<IUserService, UserService>();
|
|
|
|
#endregion
|
|
|
|
#region IMessageConsumer services
|
|
|
|
services.AddSingleton<IWorkloadCacheBuilderService, WorkloadCacheBuilderService>();
|
|
|
|
#endregion
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Регистрация сервисов по управлению задчами (Task)
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="mqSettingsFactory">Настройки MQ для всех TaskTypeEnum</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
public static IServiceCollection AddTaskManagement(this IServiceCollection services, Func<IServiceProvider, TaskTypeEnum, IMqSettings> mqSettingsFactory)
|
|
{
|
|
if (mqSettingsFactory == null)
|
|
throw new ArgumentNullException(nameof(mqSettingsFactory), "Ты забыл зарегистрировать настройки MQ для Task! Бестолочь! :)");
|
|
|
|
// Регистрация хендлеров (нужно регистировать каждый отдельно)
|
|
services.AddScoped<WorkloadReportHandler>();
|
|
services.AddScoped<BaseTaskHandler>(sp => sp.GetRequiredService<WorkloadReportHandler>());// регим для фабрики хэндлеров
|
|
// Еще хэндлеры...
|
|
|
|
// Фабрика хэндлеров
|
|
services.AddScoped<ITaskHandlerFactory, TaskHandlerFactory>();
|
|
|
|
services.AddScoped<ITaskManagementService, TaskManagementService>();
|
|
|
|
// Провайдер настроек MQ
|
|
services.AddTransient<Func<TaskTypeEnum, IMqSettings>>(sp => (taskType) => mqSettingsFactory(sp, taskType)); // регим фабрику настроек для ITaskMqSettingsProvider
|
|
services.AddSingleton<ITaskMqSettingsProvider, TaskMqSettingsProvider>();
|
|
|
|
return services;
|
|
}
|
|
|
|
|
|
private static IServiceCollection AddBllMapping(this IServiceCollection services)
|
|
{
|
|
// AutoMapper
|
|
services.AddAutoMapper(cfg =>
|
|
{
|
|
// регим все в Core
|
|
cfg.AddMaps(typeof(DependencyInjection).Assembly);
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Регистрация настроек
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <returns></returns>
|
|
private static IServiceCollection RegisterSettings(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var groupedShortcodesCacheSettings = new GroupedShortcodesCacheSettings();
|
|
configuration.GetSection(nameof(GroupedShortcodesCacheSettings)).Bind(groupedShortcodesCacheSettings);
|
|
services.AddSingleton(groupedShortcodesCacheSettings);
|
|
|
|
return services;
|
|
}
|
|
|
|
}
|
|
}
|