| | | 1 | | using ArturRios.Mediator.Command; |
| | | 2 | | using ArturRios.Mediator.Query; |
| | | 3 | | using ArturRios.Output; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | |
| | | 6 | | namespace ArturRios.Mediator; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A unified entry point that combines the command and query mediators behind a single |
| | | 10 | | /// type, enabling a CQRS-style separation of writes (commands) and reads (queries) while |
| | | 11 | | /// only depending on one mediator. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Register this type in your dependency injection container together with your command |
| | | 15 | | /// and query handlers, then inject it wherever both commands and queries need to be |
| | | 16 | | /// dispatched. Internally it delegates to a <see cref="CommandMediator"/> and a |
| | | 17 | | /// <see cref="QueryMediator"/>, both built from the supplied scope factory. |
| | | 18 | | /// </remarks> |
| | | 19 | | /// <param name="scopeFactory"> |
| | | 20 | | /// The factory used to create dependency injection scopes for resolving handlers. |
| | | 21 | | /// </param> |
| | 6 | 22 | | public class CommandQueryMediator(IServiceScopeFactory scopeFactory) |
| | | 23 | | { |
| | 6 | 24 | | private readonly CommandMediator _commandMediator = new(scopeFactory); |
| | 6 | 25 | | private readonly QueryMediator _queryMediator = new(scopeFactory); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Executes a command synchronously by delegating to the underlying <see cref="CommandMediator"/>. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <typeparam name="TCommand">The command type to execute.</typeparam> |
| | | 31 | | /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam> |
| | | 32 | | /// <param name="command">The command instance to execute.</param> |
| | | 33 | | /// <returns>The <see cref="DataOutput{T}"/> returned by the resolved handler.</returns> |
| | | 34 | | public DataOutput<TOutput?> ExecuteCommand<TCommand, TOutput>(TCommand command) |
| | | 35 | | where TCommand : BaseCommand |
| | | 36 | | where TOutput : CommandOutput |
| | 1 | 37 | | { |
| | 1 | 38 | | return _commandMediator.ExecuteCommand<TCommand, TOutput>(command); |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Executes a command asynchronously by delegating to the underlying <see cref="CommandMediator"/>. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <typeparam name="TCommand">The command type to execute.</typeparam> |
| | | 45 | | /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam> |
| | | 46 | | /// <param name="command">The command instance to execute.</param> |
| | | 47 | | /// <returns> |
| | | 48 | | /// A task that resolves to the <see cref="DataOutput{T}"/> returned by the resolved handler. |
| | | 49 | | /// </returns> |
| | | 50 | | public async Task<DataOutput<TOutput?>> ExecuteCommandAsync<TCommand, TOutput>(TCommand command) |
| | | 51 | | where TCommand : BaseCommand |
| | | 52 | | where TOutput : CommandOutput |
| | 1 | 53 | | { |
| | 1 | 54 | | return await _commandMediator.ExecuteCommandAsync<TCommand, TOutput>(command); |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Executes a paginated query synchronously by delegating to the underlying <see cref="QueryMediator"/>. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <typeparam name="TQuery">The query type to execute.</typeparam> |
| | | 61 | | /// <typeparam name="TOutput">The type of the items in the paginated result.</typeparam> |
| | | 62 | | /// <param name="query">The query instance to execute, including the requested page metadata.</param> |
| | | 63 | | /// <returns>The <see cref="PaginatedOutput{T}"/> returned by the resolved handler.</returns> |
| | | 64 | | public PaginatedOutput<TOutput> ExecutePaginatedQuery<TQuery, TOutput>(TQuery query) |
| | | 65 | | where TQuery : BaseQuery |
| | | 66 | | where TOutput : QueryOutput |
| | 1 | 67 | | { |
| | 1 | 68 | | return _queryMediator.ExecutePaginatedQuery<TQuery, TOutput>(query); |
| | 1 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Executes a paginated query asynchronously by delegating to the underlying <see cref="QueryMediator"/>. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <typeparam name="TQuery">The query type to execute.</typeparam> |
| | | 75 | | /// <typeparam name="TOutput">The type of the items in the paginated result.</typeparam> |
| | | 76 | | /// <param name="query">The query instance to execute, including the requested page metadata.</param> |
| | | 77 | | /// <returns> |
| | | 78 | | /// A task that resolves to the <see cref="PaginatedOutput{T}"/> returned by the resolved handler. |
| | | 79 | | /// </returns> |
| | | 80 | | public async Task<PaginatedOutput<TOutput>> ExecutePaginatedQueryAsync<TQuery, TOutput>(TQuery query) |
| | | 81 | | where TQuery : BaseQuery |
| | | 82 | | where TOutput : QueryOutput |
| | 1 | 83 | | { |
| | 1 | 84 | | return await _queryMediator.ExecutePaginatedQueryAsync<TQuery, TOutput>(query); |
| | 1 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Executes a single-result query synchronously by delegating to the underlying <see cref="QueryMediator"/>. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <typeparam name="TQuery">The query type to execute.</typeparam> |
| | | 91 | | /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam> |
| | | 92 | | /// <param name="query">The query instance to execute.</param> |
| | | 93 | | /// <returns>The <see cref="DataOutput{T}"/> returned by the resolved handler.</returns> |
| | | 94 | | public DataOutput<TOutput?> ExecuteQuery<TQuery, TOutput>(TQuery query) |
| | | 95 | | where TQuery : BaseQuery |
| | | 96 | | where TOutput : QueryOutput |
| | 1 | 97 | | { |
| | 1 | 98 | | return _queryMediator.ExecuteQuery<TQuery, TOutput>(query); |
| | 1 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Executes a single-result query asynchronously by delegating to the underlying <see cref="QueryMediator"/>. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <typeparam name="TQuery">The query type to execute.</typeparam> |
| | | 105 | | /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam> |
| | | 106 | | /// <param name="query">The query instance to execute.</param> |
| | | 107 | | /// <returns> |
| | | 108 | | /// A task that resolves to the <see cref="DataOutput{T}"/> returned by the resolved handler. |
| | | 109 | | /// </returns> |
| | | 110 | | public async Task<DataOutput<TOutput?>> ExecuteQueryAsync<TQuery, TOutput>(TQuery query) |
| | | 111 | | where TQuery : BaseQuery |
| | | 112 | | where TOutput : QueryOutput |
| | 1 | 113 | | { |
| | 1 | 114 | | return await _queryMediator.ExecuteQueryAsync<TQuery, TOutput>(query); |
| | 1 | 115 | | } |
| | | 116 | | } |