< Summary

Information
Class: ArturRios.Mediator.Command.CommandMediator
Assembly: ArturRios.Mediator
File(s): D:\Repositories\dotnet-mediator\src\Command\CommandMediator.cs
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 67
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
100%
Covered methods: 3
Fully covered methods: 1
Total methods: 3
Method coverage: 100%
Full method coverage: 33.3%

Metrics

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

File(s)

D:\Repositories\dotnet-mediator\src\Command\CommandMediator.cs

#LineLine coverage
 1using ArturRios.Mediator.Command.Interfaces;
 2using ArturRios.Output;
 3using Microsoft.Extensions.DependencyInjection;
 4
 5namespace ArturRios.Mediator.Command;
 6
 7/// <summary>
 8/// Dispatches commands 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.
 11/// </summary>
 12/// <remarks>
 13/// Register this mediator and the command handlers in your dependency injection
 14/// container. Because a fresh scope is created per execution, scoped dependencies of a
 15/// handler (such as a database context) are isolated to a single command execution.
 16/// </remarks>
 17/// <param name="scopeFactory">
 18/// The factory used to create a dependency injection scope for resolving handlers.
 19/// </param>
 1220public class CommandMediator(IServiceScopeFactory scopeFactory)
 21{
 22    /// <summary>
 23    /// Resolves the synchronous handler for the given command and executes it.
 24    /// </summary>
 25    /// <typeparam name="TCommand">The command type to execute.</typeparam>
 26    /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam>
 27    /// <param name="command">The command instance to execute.</param>
 28    /// <returns>The <see cref="DataOutput{T}"/> returned by the resolved handler.</returns>
 29    /// <exception cref="InvalidOperationException">
 30    /// Thrown when no <see cref="ICommandHandler{TCommand, TOutput}"/> is registered for the
 31    /// requested command and output types.
 32    /// </exception>
 33    public DataOutput<TOutput?> ExecuteCommand<TCommand, TOutput>(TCommand command)
 34        where TCommand : BaseCommand
 35        where TOutput : CommandOutput
 636    {
 637        using var scoped = scopeFactory.CreateScope();
 38
 639        var handler = scoped.ServiceProvider.GetRequiredService<ICommandHandler<TCommand, TOutput>>();
 40
 541        return handler.Handle(command);
 542    }
 43
 44    /// <summary>
 45    /// Resolves the asynchronous handler for the given command and executes it.
 46    /// </summary>
 47    /// <typeparam name="TCommand">The command type to execute.</typeparam>
 48    /// <typeparam name="TOutput">The type of the data payload produced by the handler.</typeparam>
 49    /// <param name="command">The command instance to execute.</param>
 50    /// <returns>
 51    /// A task that resolves to the <see cref="DataOutput{T}"/> returned by the resolved handler.
 52    /// </returns>
 53    /// <exception cref="InvalidOperationException">
 54    /// Thrown when no <see cref="ICommandHandlerAsync{TCommand, TOutput}"/> is registered for the
 55    /// requested command and output types.
 56    /// </exception>
 57    public async Task<DataOutput<TOutput?>> ExecuteCommandAsync<TCommand, TOutput>(TCommand command)
 58        where TCommand : BaseCommand
 59        where TOutput : CommandOutput
 360    {
 361        using var scoped = scopeFactory.CreateScope();
 62
 363        var handler = scoped.ServiceProvider.GetRequiredService<ICommandHandlerAsync<TCommand, TOutput>>();
 64
 265        return await handler.HandleAsync(command);
 266    }
 67}