| | | 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> |
| | 12 | 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 |
| | 3 | 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 | | /// <returns> |
| | | 51 | | /// A task that resolves to the <see cref="PaginatedOutput{T}"/> returned by the resolved handler. |
| | | 52 | | /// </returns> |
| | | 53 | | /// <exception cref="InvalidOperationException"> |
| | | 54 | | /// Thrown when no <see cref="IPaginatedQueryHandlerAsync{TQuery, TOutput}"/> is registered for the |
| | | 55 | | /// requested query and output types. |
| | | 56 | | /// </exception> |
| | | 57 | | public async Task<PaginatedOutput<TOutput>> ExecutePaginatedQueryAsync<TQuery, TOutput>(TQuery query) |
| | | 58 | | where TQuery : BaseQuery |
| | | 59 | | where TOutput : QueryOutput |
| | 2 | 60 | | { |
| | 2 | 61 | | using var scoped = scopeFactory.CreateScope(); |
| | | 62 | | |
| | 2 | 63 | | var handler = scoped.ServiceProvider.GetRequiredService<IPaginatedQueryHandlerAsync<TQuery, TOutput>>(); |
| | | 64 | | |
| | 2 | 65 | | return await handler.HandleAsync(query); |
| | 2 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Resolves the synchronous single-result handler for the given query and executes it. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <typeparam name="TQuery">The query type to execute.</typeparam> |
| | | 72 | | /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam> |
| | | 73 | | /// <param name="query">The query instance to execute.</param> |
| | | 74 | | /// <returns>The <see cref="DataOutput{T}"/> returned by the resolved handler.</returns> |
| | | 75 | | /// <exception cref="InvalidOperationException"> |
| | | 76 | | /// Thrown when no <see cref="IQueryHandler{TQuery, TOutput}"/> is registered for the |
| | | 77 | | /// requested query and output types. |
| | | 78 | | /// </exception> |
| | | 79 | | public DataOutput<TOutput?> ExecuteQuery<TQuery, TOutput>(TQuery query) |
| | | 80 | | where TQuery : BaseQuery |
| | | 81 | | where TOutput : QueryOutput |
| | 3 | 82 | | { |
| | 3 | 83 | | using var scoped = scopeFactory.CreateScope(); |
| | | 84 | | |
| | 3 | 85 | | var handler = scoped.ServiceProvider.GetRequiredService<IQueryHandler<TQuery, TOutput>>(); |
| | | 86 | | |
| | 2 | 87 | | return handler.Handle(query); |
| | 2 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Resolves the asynchronous single-result handler for the given query and executes it. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <typeparam name="TQuery">The query type to execute.</typeparam> |
| | | 94 | | /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam> |
| | | 95 | | /// <param name="query">The query instance to execute.</param> |
| | | 96 | | /// <returns> |
| | | 97 | | /// A task that resolves to the <see cref="DataOutput{T}"/> returned by the resolved handler. |
| | | 98 | | /// </returns> |
| | | 99 | | /// <exception cref="InvalidOperationException"> |
| | | 100 | | /// Thrown when no <see cref="IQueryHandlerAsync{TQuery, TOutput}"/> is registered for the |
| | | 101 | | /// requested query and output types. |
| | | 102 | | /// </exception> |
| | | 103 | | public async Task<DataOutput<TOutput?>> ExecuteQueryAsync<TQuery, TOutput>(TQuery query) |
| | | 104 | | where TQuery : BaseQuery |
| | | 105 | | where TOutput : QueryOutput |
| | 2 | 106 | | { |
| | 2 | 107 | | using var scoped = scopeFactory.CreateScope(); |
| | | 108 | | |
| | 2 | 109 | | var handler = scoped.ServiceProvider.GetRequiredService<IQueryHandlerAsync<TQuery, TOutput>>(); |
| | | 110 | | |
| | 2 | 111 | | return await handler.HandleAsync(query); |
| | 2 | 112 | | } |
| | | 113 | | } |