| | | 1 | | using ArturRios.Mediator.Query.Interfaces; |
| | | 2 | | using ArturRios.Output; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Mediator.Query; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Dispatches queries 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. Supports both single-result and paginated queries. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Register this mediator and the query handlers in your dependency injection container. |
| | | 14 | | /// Because a fresh scope is created per execution, scoped dependencies of a handler |
| | | 15 | | /// (such as a database context) are isolated to a single query execution. |
| | | 16 | | /// </remarks> |
| | | 17 | | /// <param name="scopeFactory"> |
| | | 18 | | /// The factory used to create a dependency injection scope for resolving handlers. |
| | | 19 | | /// </param> |
| | 27 | 20 | | public class QueryMediator(IServiceScopeFactory scopeFactory) |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Resolves the synchronous paginated handler for the given query and executes it. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <typeparam name="TQuery">The query type to execute.</typeparam> |
| | | 26 | | /// <typeparam name="TOutput">The type of the items in the paginated result.</typeparam> |
| | | 27 | | /// <param name="query">The query instance to execute, including the requested page metadata.</param> |
| | | 28 | | /// <returns>The <see cref="PaginatedOutput{T}"/> returned by the resolved handler.</returns> |
| | | 29 | | /// <exception cref="InvalidOperationException"> |
| | | 30 | | /// Thrown when no <see cref="IPaginatedQueryHandler{TQuery, TOutput}"/> is registered for the |
| | | 31 | | /// requested query and output types. |
| | | 32 | | /// </exception> |
| | | 33 | | public PaginatedOutput<TOutput> ExecutePaginatedQuery<TQuery, TOutput>(TQuery query) |
| | | 34 | | where TQuery : BaseQuery |
| | | 35 | | where TOutput : QueryOutput |
| | | 36 | | { |
| | 3 | 37 | | using var scoped = scopeFactory.CreateScope(); |
| | | 38 | | |
| | 3 | 39 | | var handler = scoped.ServiceProvider.GetRequiredService<IPaginatedQueryHandler<TQuery, TOutput>>(); |
| | | 40 | | |
| | 2 | 41 | | return handler.Handle(query); |
| | 2 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Resolves the asynchronous paginated handler for the given query and executes it. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <typeparam name="TQuery">The query type to execute.</typeparam> |
| | | 48 | | /// <typeparam name="TOutput">The type of the items in the paginated result.</typeparam> |
| | | 49 | | /// <param name="query">The query instance to execute, including the requested page metadata.</param> |
| | | 50 | | /// <param name="cancellationToken">Cancels the execution, and the dispatch itself.</param> |
| | | 51 | | /// <returns> |
| | | 52 | | /// A task that resolves to the <see cref="PaginatedOutput{T}"/> returned by the resolved handler. |
| | | 53 | | /// </returns> |
| | | 54 | | /// <exception cref="InvalidOperationException"> |
| | | 55 | | /// Thrown when no <see cref="IPaginatedQueryHandlerAsync{TQuery, TOutput}"/> is registered for the |
| | | 56 | | /// requested query and output types. |
| | | 57 | | /// </exception> |
| | | 58 | | /// <exception cref="OperationCanceledException"> |
| | | 59 | | /// <paramref name="cancellationToken"/> was already canceled. The check happens before the scope is |
| | | 60 | | /// created, so a canceled dispatch costs nothing. |
| | | 61 | | /// </exception> |
| | | 62 | | public async Task<PaginatedOutput<TOutput>> ExecutePaginatedQueryAsync<TQuery, TOutput>( |
| | | 63 | | TQuery query, |
| | | 64 | | CancellationToken cancellationToken = default) |
| | | 65 | | where TQuery : BaseQuery |
| | | 66 | | where TOutput : QueryOutput |
| | | 67 | | { |
| | 7 | 68 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 69 | | |
| | 6 | 70 | | using var scoped = scopeFactory.CreateScope(); |
| | | 71 | | |
| | 6 | 72 | | var handler = scoped.ServiceProvider.GetRequiredService<IPaginatedQueryHandlerAsync<TQuery, TOutput>>(); |
| | | 73 | | |
| | 6 | 74 | | return await handler.HandleAsync(query, cancellationToken).ConfigureAwait(false); |
| | 6 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Resolves the synchronous single-result handler for the given query and executes it. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <typeparam name="TQuery">The query type to execute.</typeparam> |
| | | 81 | | /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam> |
| | | 82 | | /// <param name="query">The query instance to execute.</param> |
| | | 83 | | /// <returns>The <see cref="DataOutput{T}"/> returned by the resolved handler.</returns> |
| | | 84 | | /// <exception cref="InvalidOperationException"> |
| | | 85 | | /// Thrown when no <see cref="IQueryHandler{TQuery, TOutput}"/> is registered for the |
| | | 86 | | /// requested query and output types. |
| | | 87 | | /// </exception> |
| | | 88 | | public DataOutput<TOutput?> ExecuteQuery<TQuery, TOutput>(TQuery query) |
| | | 89 | | where TQuery : BaseQuery |
| | | 90 | | where TOutput : QueryOutput |
| | | 91 | | { |
| | 3 | 92 | | using var scoped = scopeFactory.CreateScope(); |
| | | 93 | | |
| | 3 | 94 | | var handler = scoped.ServiceProvider.GetRequiredService<IQueryHandler<TQuery, TOutput>>(); |
| | | 95 | | |
| | 2 | 96 | | return handler.Handle(query); |
| | 2 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Resolves the asynchronous single-result handler for the given query and executes it. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <typeparam name="TQuery">The query type to execute.</typeparam> |
| | | 103 | | /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam> |
| | | 104 | | /// <param name="query">The query instance to execute.</param> |
| | | 105 | | /// <param name="cancellationToken">Cancels the execution, and the dispatch itself.</param> |
| | | 106 | | /// <returns> |
| | | 107 | | /// A task that resolves to the <see cref="DataOutput{T}"/> returned by the resolved handler. |
| | | 108 | | /// </returns> |
| | | 109 | | /// <exception cref="InvalidOperationException"> |
| | | 110 | | /// Thrown when no <see cref="IQueryHandlerAsync{TQuery, TOutput}"/> is registered for the |
| | | 111 | | /// requested query and output types. |
| | | 112 | | /// </exception> |
| | | 113 | | /// <exception cref="OperationCanceledException"> |
| | | 114 | | /// <paramref name="cancellationToken"/> was already canceled. The check happens before the scope is |
| | | 115 | | /// created, so a canceled dispatch costs nothing. |
| | | 116 | | /// </exception> |
| | | 117 | | public async Task<DataOutput<TOutput?>> ExecuteQueryAsync<TQuery, TOutput>( |
| | | 118 | | TQuery query, |
| | | 119 | | CancellationToken cancellationToken = default) |
| | | 120 | | where TQuery : BaseQuery |
| | | 121 | | where TOutput : QueryOutput |
| | | 122 | | { |
| | 7 | 123 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 124 | | |
| | 6 | 125 | | using var scoped = scopeFactory.CreateScope(); |
| | | 126 | | |
| | 6 | 127 | | var handler = scoped.ServiceProvider.GetRequiredService<IQueryHandlerAsync<TQuery, TOutput>>(); |
| | | 128 | | |
| | 6 | 129 | | return await handler.HandleAsync(query, cancellationToken).ConfigureAwait(false); |
| | 6 | 130 | | } |
| | | 131 | | } |