< 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>
 44132    public DefaultMediator(
 44133        IRequestPipeline requestPipeline,
 44134        ICommandPipeline commandPipeline,
 44135        INotificationPipeline notificationPipeline,
 44136        IOptions<MediatorOptions> options,
 44137        IServiceProvider serviceProvider)
 38    {
 44139        _requestPipeline = requestPipeline;
 44140        _commandPipeline = commandPipeline;
 44141        _notificationPipeline = notificationPipeline;
 44142        _serviceProvider = serviceProvider;
 44143        _defaultPublishingStrategy = options.Value.DefaultPublishingStrategy;
 44144        _defaultCommandStrategy = options.Value.DefaultCommandStrategy;
 44145    }
 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    {
 2559        var resultType = typeof(T);
 2560        strategy ??= _defaultCommandStrategy;
 2561        var context = new CommandContext(command, strategy, resultType, headers, _serviceProvider, cancellationToken);
 2562        await _commandPipeline.InvokeAsync(context);
 63
 2564        return (T)context.Result!;
 2565    }
 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    {
 3678        var resultType = typeof(Unit);
 3679        strategy ??= _defaultCommandStrategy;
 3680        var context = new CommandContext(command, strategy, resultType, headers, _serviceProvider, cancellationToken);
 3681        await _commandPipeline.InvokeAsync(context);
 3682    }
 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    {
 13496104        await SendAsync(notification, _defaultPublishingStrategy, cancellationToken);
 13496105    }
 106
 107    /// <inheritdoc />
 108    public async Task SendAsync(INotification notification, IEventPublishingStrategy? strategy = null, CancellationToken
 109    {
 13496110        strategy ??= _defaultPublishingStrategy;
 13496111        var context = new NotificationContext(notification, strategy, _serviceProvider, cancellationToken);
 13496112        await _notificationPipeline.ExecuteAsync(context);
 13496113    }
 114}