using FluentValidation;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PARR.Core.Common.Helpers;
using PARR.Core.Common.Implementations;
using PARR.Core.Common.Interfaces;
using PARR.Core.Services.MatchingStatusService;
using PARR.Core.Services.NextRunServices;
using PARR.Core.Services.NextRunServices.Subservices;
using PARR.Core.Services.RobotTask.Implementations;
using PARR.Core.Services.RobotTask.Interfaces;
using PARR.Core.Services.Shortcodes;
using PARR.Core.Services.Shortcodes.Handlers;
using PARR.Core.Services.TaskServices.Handlers;
using PARR.Core.Services.TaskServices.Handlers.Factory;
using PARR.Core.Services.TaskServices.Implementations;
using PARR.Core.Services.TaskServices.Interfaces;
using PARR.Core.Services.TaskServices.Providers;
using PARR.Core.Services.TaskServices.ReconciliationHosted;
using PARR.Core.Services.UnitFilterService;
using PARR.Core.Services.UnitFilterService.Models;
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
{
///
/// Регистрация Core (BLL) сервисов
///
///
///
///
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();
#region Helpers
services.AddScoped();
services.AddScoped();
#endregion
#endregion
#region Task
// ---------> Регистрируем в AddTaskManagement <---------
//// Регистрация хендлеров (нужно регистировать каждый отдельно)
//services.AddScoped();
//services.AddScoped(sp => sp.GetRequiredService());// регим для фабрики
//// Еще хэндлеры...
//// Фабрика хэндлеров
//services.AddScoped();
//services.AddScoped();
//// Провайдер настроек MQ
//services.AddSingleton();
#endregion
#region Shortсodes
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
#endregion
#region Services
services.AddTransient();
services.AddScoped();
//services.AddScoped();
#endregion
#region Workload
services.AddScoped();
services.AddScoped();
services.AddSingleton();
#endregion
#region NextRun
services.AddTransient();
services.AddTransient();
services.AddTransient();
#endregion
#region UnitFilterService
services.AddTransient();
services.Configure(options =>
{
options.LoadBatchSize = 50;
});
#endregion
#region IMessageConsumer services
services.AddSingleton();
#endregion
return services;
}
///
/// Регистрация сервисов по управлению задчами (Task)
///
///
/// Настройки MQ для всех TaskTypeEnum
///
///
public static IServiceCollection AddTaskManagement(this IServiceCollection services, Func mqSettingsFactory)
{
if (mqSettingsFactory == null)
throw new ArgumentNullException(nameof(mqSettingsFactory), "Ты забыл зарегистрировать настройки MQ для Task! Бестолочь! :)");
// Регистрация хендлеров (нужно регистировать каждый отдельно)
services.AddScoped();
services.AddScoped(sp => sp.GetRequiredService());// регим для фабрики хэндлеров
// Еще хэндлеры...
// Фабрика хэндлеров
services.AddScoped();
services.AddScoped();
// Провайдер настроек MQ
services.AddTransient>(sp => (taskType) => mqSettingsFactory(sp, taskType)); // регим фабрику настроек для ITaskMqSettingsProvider
services.AddSingleton();
return services;
}
///
/// Подключение сервисов для TaskReconciliation
///
///
///
///
public static IServiceCollection AddTaskReconciliation(this IServiceCollection services, IReconciliationSettings reconciliationSettings)
{
if (reconciliationSettings == null)
throw new ArgumentNullException(nameof(reconciliationSettings));
// Настройки
services.AddSingleton(reconciliationSettings);
// Сервис
services.AddScoped();
// Воркер
services.AddHostedService();
return services;
}
private static IServiceCollection AddBllMapping(this IServiceCollection services)
{
// AutoMapper
services.AddAutoMapper(cfg =>
{
// регим все в Core
cfg.AddMaps(typeof(DependencyInjection).Assembly);
});
return services;
}
///
/// Регистрация настроек
///
///
///
///
private static IServiceCollection RegisterSettings(this IServiceCollection services, IConfiguration configuration)
{
var groupedShortcodesCacheSettings = new GroupedShortcodesCacheSettings();
configuration.GetSection(nameof(GroupedShortcodesCacheSettings)).Bind(groupedShortcodesCacheSettings);
services.AddSingleton(groupedShortcodesCacheSettings);
return services;
}
}
}