| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using RoleRadar.Application.Abstractions; |
| | | 5 | | using RoleRadar.Application.Decorators; |
| | | 6 | | using RoleRadar.Application.Notes.Commands; |
| | | 7 | | using RoleRadar.Application.Notes.Queries; |
| | | 8 | | using RoleRadar.Domain.Common; |
| | | 9 | | |
| | | 10 | | namespace RoleRadar.Application; |
| | | 11 | | |
| | | 12 | | public static class DependencyInjection |
| | | 13 | | { |
| | | 14 | | public static IServiceCollection AddRoleRadarApplication(this IServiceCollection services) |
| | | 15 | | { |
| | 846 | 16 | | ArgumentNullException.ThrowIfNull(services); |
| | 846 | 17 | | services.TryAddSingleton<IUtcClock>(SystemUtcClock.Instance); |
| | | 18 | | |
| | | 19 | | // No request context unless the host replaces it (API, CLI and MCP do; feature 013, plan D6). |
| | 846 | 20 | | services.TryAddScoped<IRequestContext, NullRequestContext>(); |
| | | 21 | | |
| | | 22 | | // US1 & US4 Validators |
| | 846 | 23 | | services.AddSingleton<ICommandValidator<CreateNoteCommand>, CreateNoteCommandValidator>(); |
| | 846 | 24 | | services.AddSingleton<ICommandValidator<AppendToNoteCommand>, AppendToNoteCommandValidator>(); |
| | 846 | 25 | | services.AddSingleton<ICommandValidator<ReplaceNoteSectionCommand>, ReplaceNoteSectionCommandValidator>(); |
| | 846 | 26 | | services.AddSingleton<ICommandValidator<ArchiveNoteCommand>, ArchiveNoteCommandValidator>(); |
| | 846 | 27 | | services.AddSingleton<ICommandValidator<Import.Commands.ImportMarkdownCommand>, Import.Commands.ImportMarkdownCo |
| | | 28 | | |
| | | 29 | | // US1, US2 & US4 Command Handlers (wrapped by TransactionCommandDecorator + LoggingCommandDecorator) |
| | 846 | 30 | | services.AddCommandHandler<CreateNoteCommand, NoteCreatedDto, CreateNoteCommandHandler>(); |
| | 846 | 31 | | services.AddCommandHandler<AppendToNoteCommand, NoteUpdatedDto, AppendToNoteCommandHandler>(); |
| | 846 | 32 | | services.AddCommandHandler<ReplaceNoteSectionCommand, NoteSectionReplacedDto, ReplaceNoteSectionCommandHandler>( |
| | 846 | 33 | | services.AddCommandHandler<ArchiveNoteCommand, NoteArchivedDto, ArchiveNoteCommandHandler>(); |
| | 846 | 34 | | services.AddCommandHandler<Import.Commands.ImportMarkdownCommand, Import.Commands.ImportMarkdownSummaryDto, Impo |
| | | 35 | | |
| | | 36 | | // US1 Query Handlers (wrapped by LoggingQueryDecorator) |
| | 846 | 37 | | services.AddQueryHandler<GetNoteQuery, NoteDetailDto, GetNoteQueryHandler>(); |
| | 846 | 38 | | services.AddQueryHandler<SearchNotesQuery, NoteSearchResponseDto, SearchNotesQueryHandler>(); |
| | | 39 | | |
| | | 40 | | // US1 Tracking Commands and Queries |
| | 846 | 41 | | services.AddSingleton<ICommandValidator<Tracking.Commands.RecordPostingCommand>, Tracking.Commands.RecordPosting |
| | 846 | 42 | | services.AddSingleton<ICommandValidator<Tracking.Commands.TransitionPostingCommand>, Tracking.Commands.Transitio |
| | 846 | 43 | | services.AddSingleton<ICommandValidator<Tracking.Commands.RecordCompanyCommand>, Tracking.Commands.RecordCompany |
| | 846 | 44 | | services.AddSingleton<ICommandValidator<Tracking.Commands.RecordApplicationCommand>, Tracking.Commands.RecordApp |
| | 846 | 45 | | services.AddCommandHandler<Tracking.Commands.RecordPostingCommand, Tracking.Commands.PostingRecordResultDto, Tra |
| | 846 | 46 | | services.AddCommandHandler<Tracking.Commands.TransitionPostingCommand, Tracking.Commands.PostingTransitionResult |
| | 846 | 47 | | services.AddCommandHandler<Tracking.Commands.RecordCompanyCommand, Tracking.Commands.CompanyRecordResultDto, Tra |
| | 846 | 48 | | services.AddCommandHandler<Tracking.Commands.TrackerLogCommand, Tracking.Commands.TrackerLogResultDto, Tracking. |
| | 846 | 49 | | services.AddCommandHandler<Tracking.Commands.RecordApplicationCommand, Tracking.Commands.ApplicationRecordResult |
| | 846 | 50 | | services.AddCommandHandler<Tracking.Commands.AddContactCommand, Tracking.Commands.ContactResultDto, Tracking.Com |
| | 846 | 51 | | services.AddCommandHandler<Tracking.Commands.RedactContactCommand, Tracking.Commands.ContactRedactResultDto, Tra |
| | 846 | 52 | | services.AddQueryHandler<Tracking.Queries.TrackerQueryRequest, Tracking.Queries.TrackerQueryResultDto, Tracking. |
| | 846 | 53 | | services.AddQueryHandler<Tracking.Queries.GetContactQuery, Tracking.Queries.ContactDto?, Tracking.Queries.GetCon |
| | 846 | 54 | | services.AddQueryHandler<Integrity.VerifyIntegrityQuery, Integrity.VerifyIntegrityResultDto, Integrity.VerifyInt |
| | | 55 | | |
| | | 56 | | // Feature 007: Insights stats read (a query: no transaction, no audit entry). |
| | 846 | 57 | | services.AddQueryHandler<Insights.GetInsightsStatsQuery, Insights.InsightsStatsDto, Insights.GetInsightsStatsQue |
| | | 58 | | |
| | | 59 | | // Feature 013: the read-only audit listing behind `roleradar audit list` (a query: no transaction, no audit ent |
| | 846 | 60 | | services.AddQueryHandler<Audit.Queries.ListAuditEntriesQuery, Audit.Queries.AuditListPage, Audit.Queries.ListAud |
| | | 61 | | |
| | | 62 | | // Feature 004: Tracker Import |
| | 846 | 63 | | services.AddTransient<Tracking.Import.Pipeline.ITrackerImportPipelineFactory, Tracking.Import.Pipeline.TrackerIm |
| | 846 | 64 | | services.AddTransient<Tracking.Import.Commands.ITrackerImportCoordinator, Tracking.Import.Commands.TrackerImport |
| | | 65 | | |
| | 846 | 66 | | return services; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public static IServiceCollection AddCommandHandler<TCommand, TResponse, THandler>(this IServiceCollection services) |
| | | 70 | | where TCommand : ICommand<TResponse> |
| | | 71 | | where THandler : class, ICommandHandler<TCommand, TResponse> |
| | | 72 | | { |
| | 10154 | 73 | | ArgumentNullException.ThrowIfNull(services); |
| | | 74 | | |
| | 10154 | 75 | | services.AddScoped<THandler>(); |
| | 10154 | 76 | | services.AddScoped<ICommandHandler<TCommand, TResponse>>(sp => |
| | 10154 | 77 | | { |
| | 2276 | 78 | | var inner = sp.GetRequiredService<THandler>(); |
| | 2276 | 79 | | var repository = sp.GetRequiredService<INoteRepository>(); |
| | 2276 | 80 | | var validators = sp.GetServices<ICommandValidator<TCommand>>(); |
| | 2276 | 81 | | var logger = sp.GetRequiredService<ILogger<LoggingCommandDecorator<TCommand, TResponse>>>(); |
| | 10154 | 82 | | |
| | 2276 | 83 | | var transactional = new TransactionCommandDecorator<TCommand, TResponse>( |
| | 2276 | 84 | | inner, |
| | 2276 | 85 | | repository, |
| | 2276 | 86 | | validators); |
| | 10154 | 87 | | |
| | 2276 | 88 | | return new LoggingCommandDecorator<TCommand, TResponse>(transactional, logger); |
| | 10154 | 89 | | }); |
| | | 90 | | |
| | 10154 | 91 | | return services; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public static IServiceCollection AddQueryHandler<TQuery, TResponse, THandler>(this IServiceCollection services) |
| | | 95 | | where TQuery : IQuery<TResponse> |
| | | 96 | | where THandler : class, IQueryHandler<TQuery, TResponse> |
| | | 97 | | { |
| | 5922 | 98 | | ArgumentNullException.ThrowIfNull(services); |
| | | 99 | | |
| | 5922 | 100 | | services.AddScoped<THandler>(); |
| | 5922 | 101 | | services.AddScoped<IQueryHandler<TQuery, TResponse>>(sp => |
| | 5922 | 102 | | { |
| | 690 | 103 | | var inner = sp.GetRequiredService<THandler>(); |
| | 690 | 104 | | var logger = sp.GetRequiredService<ILogger<LoggingQueryDecorator<TQuery, TResponse>>>(); |
| | 690 | 105 | | return new LoggingQueryDecorator<TQuery, TResponse>(inner, logger); |
| | 5922 | 106 | | }); |
| | | 107 | | |
| | 5922 | 108 | | return services; |
| | | 109 | | } |
| | | 110 | | } |