| | | 1 | | using ArturRios.Mediator.Command; |
| | | 2 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Heimdall.Command.Auditing; |
| | | 6 | | |
| | | 7 | | public static class CommandHandlerRegistrationExtensions |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Registers <typeparamref name="THandler" /> and wraps it with <see cref="AuditingCommandHandler{TCommand,TOut |
| | | 11 | | /// so every command handler produces an audit trail entry on success (NFR-09), without any |
| | | 12 | | /// change to the handler itself. |
| | | 13 | | /// The concrete <typeparamref name="THandler" /> registration this method also creates is an |
| | | 14 | | /// implementation detail, kept only so the decorator factory above can resolve it; callers should |
| | | 15 | | /// always depend on <see cref="ICommandHandlerAsync{TCommand,TOutput}" /> and never resolve |
| | | 16 | | /// <typeparamref name="THandler" /> directly, or auditing is silently bypassed. |
| | | 17 | | /// </summary> |
| | | 18 | | public static IServiceCollection AddAuditedCommandHandler<TCommand, TOutput, THandler>( |
| | | 19 | | this IServiceCollection services) |
| | | 20 | | where TCommand : BaseCommand |
| | | 21 | | where TOutput : CommandOutput |
| | | 22 | | where THandler : class, ICommandHandlerAsync<TCommand, TOutput> |
| | | 23 | | { |
| | 48546 | 24 | | services.AddScoped<THandler>(); |
| | 48546 | 25 | | services.AddScoped<ICommandHandlerAsync<TCommand, TOutput>>(provider => |
| | 49525 | 26 | | new AuditingCommandHandler<TCommand, TOutput>( |
| | 49525 | 27 | | provider.GetRequiredService<THandler>(), |
| | 49525 | 28 | | provider.GetRequiredService<IAuditLogWriter>(), |
| | 49525 | 29 | | provider.GetRequiredService< |
| | 49525 | 30 | | Microsoft.Extensions.Logging.ILogger<AuditingCommandHandler<TCommand, TOutput>>>())); |
| | | 31 | | |
| | 48546 | 32 | | return services; |
| | | 33 | | } |
| | | 34 | | } |