< Summary

Information
Class: ArturRios.Mediator.CommandQueryMediator
Assembly: ArturRios.Mediator
File(s): /home/ci/agent/workspace/dotnet-mediator-docs_main/app/src/CommandQueryMediator.cs
Line coverage
100%
Covered lines: 18
Uncovered lines: 0
Coverable lines: 18
Total lines: 148
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
100%
Covered methods: 7
Fully covered methods: 7
Total methods: 7
Method coverage: 100%
Full method coverage: 100%

Metrics

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

File(s)

/home/ci/agent/workspace/dotnet-mediator-docs_main/app/src/CommandQueryMediator.cs

#LineLine coverage
 1using ArturRios.Mediator.Command;
 2using ArturRios.Mediator.Command.Interfaces;
 3using ArturRios.Mediator.Query;
 4using ArturRios.Mediator.Query.Interfaces;
 5using ArturRios.Output;
 6using Microsoft.Extensions.DependencyInjection;
 7
 8namespace ArturRios.Mediator;
 9
 10/// <summary>
 11/// A unified entry point that combines the command and query mediators behind a single
 12/// type, enabling a CQRS-style separation of writes (commands) and reads (queries) while
 13/// only depending on one mediator.
 14/// </summary>
 15/// <remarks>
 16/// Register this type in your dependency injection container together with your command
 17/// and query handlers, then inject it wherever both commands and queries need to be
 18/// dispatched. Internally it delegates to a <see cref="CommandMediator"/> and a
 19/// <see cref="QueryMediator"/>, both built from the supplied scope factory.
 20/// </remarks>
 21/// <param name="scopeFactory">
 22/// The factory used to create dependency injection scopes for resolving handlers.
 23/// </param>
 2924public class CommandQueryMediator(IServiceScopeFactory scopeFactory)
 25{
 2926    private readonly CommandMediator _commandMediator = new(scopeFactory);
 2927    private readonly QueryMediator _queryMediator = new(scopeFactory);
 28
 29    /// <summary>
 30    /// Executes a command synchronously by delegating to the underlying <see cref="CommandMediator"/>.
 31    /// </summary>
 32    /// <typeparam name="TCommand">The command type to execute.</typeparam>
 33    /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam>
 34    /// <param name="command">The command instance to execute.</param>
 35    /// <returns>The <see cref="DataOutput{T}"/> returned by the resolved handler.</returns>
 36    /// <exception cref="ArgumentNullException"><paramref name="command"/> is <see langword="null"/>.</exception>
 37    /// <exception cref="InvalidOperationException">No <see cref="ICommandHandler{TCommand, TOutput}"/> is registered fo
 38    public DataOutput<TOutput?> ExecuteCommand<TCommand, TOutput>(TCommand command)
 39        where TCommand : BaseCommand
 40        where TOutput : CommandOutput
 41    {
 242        return _commandMediator.ExecuteCommand<TCommand, TOutput>(command);
 43    }
 44
 45    /// <summary>
 46    /// Executes a command asynchronously by delegating to the underlying <see cref="CommandMediator"/>.
 47    /// </summary>
 48    /// <typeparam name="TCommand">The command type to execute.</typeparam>
 49    /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam>
 50    /// <param name="command">The command instance to execute.</param>
 51    /// <param name="cancellationToken">Cancels the execution, and the dispatch itself.</param>
 52    /// <returns>
 53    /// A task that resolves to the <see cref="DataOutput{T}"/> returned by the resolved handler.
 54    /// </returns>
 55    /// <exception cref="ArgumentNullException"><paramref name="command"/> is <see langword="null"/>.</exception>
 56    /// <exception cref="InvalidOperationException">No <see cref="ICommandHandlerAsync{TCommand, TOutput}"/> is register
 57    /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was already canceled.</excepti
 58    public async Task<DataOutput<TOutput?>> ExecuteCommandAsync<TCommand, TOutput>(
 59        TCommand command,
 60        CancellationToken cancellationToken = default)
 61        where TCommand : BaseCommand
 62        where TOutput : CommandOutput
 63    {
 3564        return await _commandMediator
 3565            .ExecuteCommandAsync<TCommand, TOutput>(command, cancellationToken)
 3566            .ConfigureAwait(false);
 3267    }
 68
 69    /// <summary>
 70    /// Executes a paginated query synchronously by delegating to the underlying <see cref="QueryMediator"/>.
 71    /// </summary>
 72    /// <typeparam name="TQuery">The query type to execute.</typeparam>
 73    /// <typeparam name="TOutput">The type of the items in the paginated result.</typeparam>
 74    /// <param name="query">The query instance to execute, including the requested page metadata.</param>
 75    /// <returns>The <see cref="PaginatedOutput{T}"/> returned by the resolved handler.</returns>
 76    /// <exception cref="ArgumentNullException"><paramref name="query"/> is <see langword="null"/>.</exception>
 77    /// <exception cref="InvalidOperationException">No <see cref="IPaginatedQueryHandler{TQuery, TOutput}"/> is register
 78    public PaginatedOutput<TOutput> ExecutePaginatedQuery<TQuery, TOutput>(TQuery query)
 79        where TQuery : BaseQuery
 80        where TOutput : QueryOutput
 81    {
 282        return _queryMediator.ExecutePaginatedQuery<TQuery, TOutput>(query);
 83    }
 84
 85    /// <summary>
 86    /// Executes a paginated query asynchronously by delegating to the underlying <see cref="QueryMediator"/>.
 87    /// </summary>
 88    /// <typeparam name="TQuery">The query type to execute.</typeparam>
 89    /// <typeparam name="TOutput">The type of the items in the paginated result.</typeparam>
 90    /// <param name="query">The query instance to execute, including the requested page metadata.</param>
 91    /// <param name="cancellationToken">Cancels the execution, and the dispatch itself.</param>
 92    /// <returns>
 93    /// A task that resolves to the <see cref="PaginatedOutput{T}"/> returned by the resolved handler.
 94    /// </returns>
 95    /// <exception cref="ArgumentNullException"><paramref name="query"/> is <see langword="null"/>.</exception>
 96    /// <exception cref="InvalidOperationException">No <see cref="IPaginatedQueryHandlerAsync{TQuery, TOutput}"/> is reg
 97    /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was already canceled.</excepti
 98    public async Task<PaginatedOutput<TOutput>> ExecutePaginatedQueryAsync<TQuery, TOutput>(
 99        TQuery query,
 100        CancellationToken cancellationToken = default)
 101        where TQuery : BaseQuery
 102        where TOutput : QueryOutput
 103    {
 8104        return await _queryMediator
 8105            .ExecutePaginatedQueryAsync<TQuery, TOutput>(query, cancellationToken)
 8106            .ConfigureAwait(false);
 6107    }
 108
 109    /// <summary>
 110    /// Executes a single-result query synchronously by delegating to the underlying <see cref="QueryMediator"/>.
 111    /// </summary>
 112    /// <typeparam name="TQuery">The query type to execute.</typeparam>
 113    /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam>
 114    /// <param name="query">The query instance to execute.</param>
 115    /// <returns>The <see cref="DataOutput{T}"/> returned by the resolved handler.</returns>
 116    /// <exception cref="ArgumentNullException"><paramref name="query"/> is <see langword="null"/>.</exception>
 117    /// <exception cref="InvalidOperationException">No <see cref="IQueryHandler{TQuery, TOutput}"/> is registered for th
 118    public DataOutput<TOutput?> ExecuteQuery<TQuery, TOutput>(TQuery query)
 119        where TQuery : BaseQuery
 120        where TOutput : QueryOutput
 121    {
 2122        return _queryMediator.ExecuteQuery<TQuery, TOutput>(query);
 123    }
 124
 125    /// <summary>
 126    /// Executes a single-result query asynchronously by delegating to the underlying <see cref="QueryMediator"/>.
 127    /// </summary>
 128    /// <typeparam name="TQuery">The query type to execute.</typeparam>
 129    /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam>
 130    /// <param name="query">The query instance to execute.</param>
 131    /// <param name="cancellationToken">Cancels the execution, and the dispatch itself.</param>
 132    /// <returns>
 133    /// A task that resolves to the <see cref="DataOutput{T}"/> returned by the resolved handler.
 134    /// </returns>
 135    /// <exception cref="ArgumentNullException"><paramref name="query"/> is <see langword="null"/>.</exception>
 136    /// <exception cref="InvalidOperationException">No <see cref="IQueryHandlerAsync{TQuery, TOutput}"/> is registered f
 137    /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was already canceled.</excepti
 138    public async Task<DataOutput<TOutput?>> ExecuteQueryAsync<TQuery, TOutput>(
 139        TQuery query,
 140        CancellationToken cancellationToken = default)
 141        where TQuery : BaseQuery
 142        where TOutput : QueryOutput
 143    {
 7144        return await _queryMediator
 7145            .ExecuteQueryAsync<TQuery, TOutput>(query, cancellationToken)
 7146            .ConfigureAwait(false);
 5147    }
 148}