< Summary

Information
Class: Elsa.Mediator.Services.DefaultMediator
Assembly: Elsa.Mediator
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Services/DefaultMediator.cs
Line coverage
80%
Covered lines: 32
Uncovered lines: 8
Coverable lines: 40
Total lines: 114
Line coverage: 80%
Branch coverage
50%
Covered branches: 3
Total branches: 6
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
SendAsync()100%210%
SendAsync()50%22100%
SendAsync()100%11100%
SendAsync(...)100%11100%
SendAsync()50%22100%
SendAsync(...)100%210%
SendAsync(...)100%210%
SendAsync(...)100%210%
SendAsync()100%11100%
SendAsync()50%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Services/DefaultMediator.cs

#LineLine coverage
 1using Elsa.Mediator.Contracts;
 2using Elsa.Mediator.Middleware.Command;
 3using Elsa.Mediator.Middleware.Command.Contracts;
 4using Elsa.Mediator.Middleware.Notification;
 5using Elsa.Mediator.Middleware.Notification.Contracts;
 6using Elsa.Mediator.Middleware.Request;
 7using Elsa.Mediator.Middleware.Request.Contracts;
 8using Elsa.Mediator.Models;
 9using Elsa.Mediator.Options;
 10using Microsoft.Extensions.Options;
 11
 12namespace Elsa.Mediator.Services;
 13
 14/// <inheritdoc />
 15public class DefaultMediator : IMediator
 16{
 17    private readonly IRequestPipeline _requestPipeline;
 18    private readonly ICommandPipeline _commandPipeline;
 19    private readonly INotificationPipeline _notificationPipeline;
 20    private readonly IServiceProvider _serviceProvider;
 21    private readonly IEventPublishingStrategy _defaultPublishingStrategy;
 22    private readonly ICommandStrategy _defaultCommandStrategy;
 23
 24    /// <summary>
 25    /// Initializes a new instance of the <see cref="DefaultMediator"/> class.
 26    /// </summary>
 27    /// <param name="requestPipeline">The request pipeline.</param>
 28    /// <param name="commandPipeline">The command pipeline.</param>
 29    /// <param name="notificationPipeline">The notification pipeline.</param>
 30    /// <param name="options">The mediator options.</param>
 31    /// <param name="serviceProvider">The service provider.</param>
 32232    public DefaultMediator(
 32233        IRequestPipeline requestPipeline,
 32234        ICommandPipeline commandPipeline,
 32235        INotificationPipeline notificationPipeline,
 32236        IOptions<MediatorOptions> options,
 32237        IServiceProvider serviceProvider)
 38    {
 32239        _requestPipeline = requestPipeline;
 32240        _commandPipeline = commandPipeline;
 32241        _notificationPipeline = notificationPipeline;
 32242        _serviceProvider = serviceProvider;
 32243        _defaultPublishingStrategy = options.Value.DefaultPublishingStrategy;
 32244        _defaultCommandStrategy = options.Value.DefaultCommandStrategy;
 32245    }
 46
 47    /// <inheritdoc />
 48    public async Task<T> SendAsync<T>(IRequest<T> request, CancellationToken cancellationToken = default)
 49    {
 050        var responseType = typeof(T);
 051        var context = new RequestContext(request, responseType, _serviceProvider, cancellationToken);
 052        await _requestPipeline.ExecuteAsync(context);
 53
 054        return (T)context.Response;
 055    }
 56
 57    public async Task<T> SendAsync<T>(ICommand<T> command, ICommandStrategy? strategy, IDictionary<object, object> heade
 58    {
 2259        var resultType = typeof(T);
 2260        strategy ??= _defaultCommandStrategy;
 2261        var context = new CommandContext(command, strategy, resultType, headers, _serviceProvider, cancellationToken);
 2262        await _commandPipeline.InvokeAsync(context);
 63
 2264        return (T)context.Result!;
 2265    }
 66
 67    /// <inheritdoc />
 1168    public async Task SendAsync(ICommand command, CancellationToken cancellationToken = default) => await SendAsync(comm
 69
 70    /// <inheritdoc />
 71    public Task SendAsync(ICommand command, ICommandStrategy? strategy = null, CancellationToken cancellationToken = def
 72    {
 1173        return SendAsync(command, strategy, new Dictionary<object, object>(), cancellationToken);
 74    }
 75
 76    public async Task SendAsync(ICommand command, ICommandStrategy? strategy, IDictionary<object, object> headers, Cance
 77    {
 3378        var resultType = typeof(Unit);
 3379        strategy ??= _defaultCommandStrategy;
 3380        var context = new CommandContext(command, strategy, resultType, headers, _serviceProvider, cancellationToken);
 3381        await _commandPipeline.InvokeAsync(context);
 3382    }
 83
 84    /// <inheritdoc />
 85    public Task<T> SendAsync<T>(ICommand<T> command, CancellationToken cancellationToken = default)
 86    {
 087        return SendAsync(command, new Dictionary<object, object>(), cancellationToken);
 88    }
 89
 90    public Task<T> SendAsync<T>(ICommand<T> command, IDictionary<object, object> headers, CancellationToken cancellation
 91    {
 092        return SendAsync(command, _defaultCommandStrategy, headers, cancellationToken);
 93    }
 94
 95    /// <inheritdoc />
 96    public Task<T> SendAsync<T>(ICommand<T> command, ICommandStrategy? strategy, CancellationToken cancellationToken = d
 97    {
 098        return SendAsync(command, strategy, new Dictionary<object, object>(), cancellationToken);
 99    }
 100
 101    /// <inheritdoc />
 102    public async Task SendAsync(INotification notification, CancellationToken cancellationToken = default)
 103    {
 11316104        await SendAsync(notification, _defaultPublishingStrategy, cancellationToken);
 11316105    }
 106
 107    /// <inheritdoc />
 108    public async Task SendAsync(INotification notification, IEventPublishingStrategy? strategy = null, CancellationToken
 109    {
 11316110        strategy ??= _defaultPublishingStrategy;
 11316111        var context = new NotificationContext(notification, strategy, _serviceProvider, cancellationToken);
 11316112        await _notificationPipeline.ExecuteAsync(context);
 11316113    }
 114}