< Summary

Information
Class: ArturRios.Mediator.Query.QueryMediator
Assembly: ArturRios.Mediator
File(s): D:\Repositories\dotnet-mediator\src\Query\QueryMediator.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 113
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: 1
Total methods: 5
Method coverage: 100%
Full method coverage: 20%

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)

D:\Repositories\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>
 1220public 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
 336    {
 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    /// <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
 260    {
 261        using var scoped = scopeFactory.CreateScope();
 62
 263        var handler = scoped.ServiceProvider.GetRequiredService<IPaginatedQueryHandlerAsync<TQuery, TOutput>>();
 64
 265        return await handler.HandleAsync(query);
 266    }
 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
 382    {
 383        using var scoped = scopeFactory.CreateScope();
 84
 385        var handler = scoped.ServiceProvider.GetRequiredService<IQueryHandler<TQuery, TOutput>>();
 86
 287        return handler.Handle(query);
 288    }
 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
 2106    {
 2107        using var scoped = scopeFactory.CreateScope();
 108
 2109        var handler = scoped.ServiceProvider.GetRequiredService<IQueryHandlerAsync<TQuery, TOutput>>();
 110
 2111        return await handler.HandleAsync(query);
 2112    }
 113}