| | | 1 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 2 | | using ArturRios.Output; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Mediator.Command; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Dispatches commands to their registered handlers. For each call a new dependency |
| | | 9 | | /// injection scope is created, the matching handler is resolved from it, and the |
| | | 10 | | /// handler is invoked. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Register this mediator and the command handlers in your dependency injection |
| | | 14 | | /// container. Because a fresh scope is created per execution, scoped dependencies of a |
| | | 15 | | /// handler (such as a database context) are isolated to a single command execution. |
| | | 16 | | /// </remarks> |
| | | 17 | | /// <param name="scopeFactory"> |
| | | 18 | | /// The factory used to create a dependency injection scope for resolving handlers. |
| | | 19 | | /// </param> |
| | 12 | 20 | | public class CommandMediator(IServiceScopeFactory scopeFactory) |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Resolves the synchronous handler for the given command and executes it. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <typeparam name="TCommand">The command type to execute.</typeparam> |
| | | 26 | | /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam> |
| | | 27 | | /// <param name="command">The command instance to execute.</param> |
| | | 28 | | /// <returns>The <see cref="DataOutput{T}"/> returned by the resolved handler.</returns> |
| | | 29 | | /// <exception cref="InvalidOperationException"> |
| | | 30 | | /// Thrown when no <see cref="ICommandHandler{TCommand, TOutput}"/> is registered for the |
| | | 31 | | /// requested command and output types. |
| | | 32 | | /// </exception> |
| | | 33 | | public DataOutput<TOutput?> ExecuteCommand<TCommand, TOutput>(TCommand command) |
| | | 34 | | where TCommand : BaseCommand |
| | | 35 | | where TOutput : CommandOutput |
| | 6 | 36 | | { |
| | 6 | 37 | | using var scoped = scopeFactory.CreateScope(); |
| | | 38 | | |
| | 6 | 39 | | var handler = scoped.ServiceProvider.GetRequiredService<ICommandHandler<TCommand, TOutput>>(); |
| | | 40 | | |
| | 5 | 41 | | return handler.Handle(command); |
| | 5 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Resolves the asynchronous handler for the given command and executes it. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <typeparam name="TCommand">The command type to execute.</typeparam> |
| | | 48 | | /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam> |
| | | 49 | | /// <param name="command">The command instance to execute.</param> |
| | | 50 | | /// <returns> |
| | | 51 | | /// A task that resolves to the <see cref="DataOutput{T}"/> returned by the resolved handler. |
| | | 52 | | /// </returns> |
| | | 53 | | /// <exception cref="InvalidOperationException"> |
| | | 54 | | /// Thrown when no <see cref="ICommandHandlerAsync{TCommand, TOutput}"/> is registered for the |
| | | 55 | | /// requested command and output types. |
| | | 56 | | /// </exception> |
| | | 57 | | public async Task<DataOutput<TOutput?>> ExecuteCommandAsync<TCommand, TOutput>(TCommand command) |
| | | 58 | | where TCommand : BaseCommand |
| | | 59 | | where TOutput : CommandOutput |
| | 3 | 60 | | { |
| | 3 | 61 | | using var scoped = scopeFactory.CreateScope(); |
| | | 62 | | |
| | 3 | 63 | | var handler = scoped.ServiceProvider.GetRequiredService<ICommandHandlerAsync<TCommand, TOutput>>(); |
| | | 64 | | |
| | 2 | 65 | | return await handler.HandleAsync(command); |
| | 2 | 66 | | } |
| | | 67 | | } |