< Summary

Information
Class: ArturRios.Mediator.Query.QueryMediator
Assembly: ArturRios.Mediator
File(s): /home/runner/work/dotnet-mediator/dotnet-mediator/src/Query/QueryMediator.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 131
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
100%
Covered methods: 5
Fully covered methods: 5
Total methods: 5
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ExecutePaginatedQuery(...)100%11100%
ExecutePaginatedQueryAsync()100%11100%
ExecuteQuery(...)100%11100%
ExecuteQueryAsync()100%11100%

File(s)

/home/runner/work/dotnet-mediator/dotnet-mediator/src/Query/QueryMediator.cs

#LineLine coverage
 1using ArturRios.Mediator.Query.Interfaces;
 2using ArturRios.Output;
 3using Microsoft.Extensions.DependencyInjection;
 4
 5namespace 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>
 2720public 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    {
 337        using var scoped = scopeFactory.CreateScope();
 38
 339        var handler = scoped.ServiceProvider.GetRequiredService<IPaginatedQueryHandler<TQuery, TOutput>>();
 40
 241        return handler.Handle(query);
 242    }
 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    {
 768        cancellationToken.ThrowIfCancellationRequested();
 69
 670        using var scoped = scopeFactory.CreateScope();
 71
 672        var handler = scoped.ServiceProvider.GetRequiredService<IPaginatedQueryHandlerAsync<TQuery, TOutput>>();
 73
 674        return await handler.HandleAsync(query, cancellationToken).ConfigureAwait(false);
 675    }
 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    {
 392        using var scoped = scopeFactory.CreateScope();
 93
 394        var handler = scoped.ServiceProvider.GetRequiredService<IQueryHandler<TQuery, TOutput>>();
 95
 296        return handler.Handle(query);
 297    }
 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    {
 7123        cancellationToken.ThrowIfCancellationRequested();
 124
 6125        using var scoped = scopeFactory.CreateScope();
 126
 6127        var handler = scoped.ServiceProvider.GetRequiredService<IQueryHandlerAsync<TQuery, TOutput>>();
 128
 6129        return await handler.HandleAsync(query, cancellationToken).ConfigureAwait(false);
 6130    }
 131}