- SimpleTemplateSynchronizer и GroupedTemplateSynchronizer переведены на паттерн Pipeline с разделением на Read/Write этапы - Выделены контракты этапов (ISimpleSyncStage, IGroupedSyncStage) и контексты (SimpleSyncContext, GroupedSyncContext) - Read-этапы безопасны для тестов (не пишут в БД/MQ), Write-этапы изолированы через отдельные интерфейсы - Добавлено [Perf]-логирование каждого этапа с метриками времени выполнения - Логи приведены к человекочитаемому формату 'Имя' (ID) для Job, JobGroup и Unit - Устранено дублирование данных в контекстах (FilteredUnits перезаписывается, TemplateGroups строго типизирован) - Константы неиспользуемых шаблонов вынесены в UnusedTemplateConstants - Структура проекта реорганизована: SimpleSync, GroupedSync, Implementations, Interfaces
96 lines
5.3 KiB
C#
96 lines
5.3 KiB
C#
using Microsoft.Extensions.Configuration;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using PARR.Core;
|
||
using PARR.DAL;
|
||
using PARR.Infrastructure;
|
||
using PARR.TemplateMatcher.Services.GroupedSync;
|
||
using PARR.TemplateMatcher.Services.Implementations;
|
||
using PARR.TemplateMatcher.Services.Interfaces;
|
||
using PARR.TemplateMatcher.Services.SimpleSync;
|
||
using PARR.TemplateMatcher.Settings;
|
||
|
||
namespace PARR.TemplateMatcher
|
||
{
|
||
public static class TemplateMatcherInstaller
|
||
{
|
||
public static void InstallTemplateMatcherSerivces(this IServiceCollection services, IConfiguration configuration)
|
||
{
|
||
services.AddDalServices(configuration);
|
||
services.AddCoreServices(configuration);
|
||
services.AddInfrastructureServices(configuration);
|
||
|
||
var mqSettings = new MqSettings();
|
||
configuration.GetSection(nameof(MqSettings)).Bind(mqSettings);
|
||
services.AddSingleton(mqSettings);
|
||
|
||
var templateSettings = new TemplateSettings();
|
||
configuration.GetSection(nameof(TemplateSettings)).Bind(templateSettings);
|
||
services.AddSingleton(templateSettings);
|
||
|
||
// === 1. Singleton: Долгоживущие сервисы и обработчики очередей ===
|
||
services.AddSingleton<IMqTemplateMatcher, MqTemplateMatcher>();
|
||
|
||
// === 2. Scoped: Бизнес-логика и работа с БД (DbContext) ===
|
||
// Создаются заново для каждого сообщения из очереди (внутри CreateAsyncScope)
|
||
services.AddScoped<ITemplateMatcher, Services.Implementations.TemplateMatcher>();
|
||
|
||
// Simple pipeline: Read-этапы
|
||
services.AddScoped<ISimpleSyncStage, Services.Implementations.SimpleSync.LoadJobStage>(); //1
|
||
services.AddScoped<ISimpleSyncStage, Services.Implementations.SimpleSync.FilterUnitsStage>(); //2
|
||
services.AddScoped<ISimpleSyncStage, Services.Implementations.SimpleSync.AnalyzeChangesStage>(); //3
|
||
|
||
// Simple pipeline: Write-этапы
|
||
services.AddScoped<ISimpleSyncWriteStage, Services.Implementations.SimpleSync.AllocateTemplatesStage>(); //4
|
||
services.AddScoped<ISimpleSyncWriteStage, Services.Implementations.SimpleSync.UpdateNamesStage>(); //5
|
||
services.AddScoped<ISimpleSyncWriteStage, Services.Implementations.SimpleSync.DeactivateTemplatesStage>(); //6
|
||
|
||
services.AddScoped<ITemplateSynchronizer, SimpleTemplateSynchronizer>();
|
||
|
||
// Grouped pipeline: Read-этапы
|
||
services.AddScoped<IGroupedSyncStage, Services.Implementations.GroupedSync.LoadJobGroupStage>(); //1
|
||
services.AddScoped<IGroupedSyncStage, Services.Implementations.GroupedSync.FilterUnitsStage>(); //2
|
||
services.AddScoped<IGroupedSyncStage, Services.Implementations.GroupedSync.GroupFilterStage>(); //3
|
||
services.AddScoped<IGroupedSyncStage, Services.Implementations.GroupedSync.ResolveConflictsStage>(); //4
|
||
services.AddScoped<IGroupedSyncStage, Services.Implementations.GroupedSync.BuildGroupsStage>(); //5
|
||
|
||
// Grouped pipeline: Write-этапы
|
||
services.AddScoped<IGroupedSyncWriteStage, Services.Implementations.GroupedSync.ProcessGroupsStage>(); //6
|
||
services.AddScoped<IGroupedSyncWriteStage, Services.Implementations.GroupedSync.DeactivateTemplatesStage>();//7
|
||
|
||
services.AddScoped<ITemplateSynchronizer, GroupedTemplateSynchronizer>();
|
||
|
||
// Пайплайн аллокации шаблонов
|
||
services.AddScoped<ITemplateAllocationService, TemplateAllocationService>();
|
||
services.AddScoped<ITemplateReuser, TemplateReuser>();
|
||
services.AddScoped<ITemplateDeactivator, TemplateDeactivator>();
|
||
|
||
// Пайплайн групповых шаблонов
|
||
services.AddScoped<IGroupedTemplateUnitFilter, GroupedTemplateUnitFilter>();
|
||
services.AddScoped<IUnitInTemplateConflictMapper, UnitInTemplateConflictMapper>();
|
||
services.AddScoped<IGroupedTemplateBuilder, GroupedTemplateBuilder>();
|
||
services.AddScoped<IGroupedTemplateProcessor, GroupedTemplateProcessor>();
|
||
|
||
// Валидаторы
|
||
services.AddScoped<IJobValidatorService, JobValidatorService>();
|
||
services.AddScoped<IJobGroupValidatorService, JobGroupValidatorService>();
|
||
|
||
// === 3. Transient: Stateless утилиты и инфраструктура ===
|
||
// Легковесные сервисы без состояния, создаются по требованию
|
||
services.AddTransient<ITemplateMqPublisher, TemplateMqPublisher>();
|
||
services.AddTransient<ITemplateNameNormalizer, TemplateNameNormalizer>();
|
||
}
|
||
|
||
public static IConfigurationBuilder AddTemplateMatcherConfigurations(this IConfigurationBuilder builder, IServiceCollection services)
|
||
{
|
||
builder.AddDalConfigurations(services);
|
||
|
||
return builder;
|
||
}
|
||
|
||
public static void AddTemplateMatcherSettings(this IServiceCollection services, IConfiguration configuration)
|
||
{
|
||
services.AddDallSettings(configuration);
|
||
}
|
||
}
|
||
}
|