< Summary

Information
Class: MediatR.ExtendedMediator
Assembly: Pozitron.Extensions.MediatR
File(s): /home/runner/work/Pozitron.Extensions.MediatR/Pozitron.Extensions.MediatR/src/Pozitron.Extensions.MediatR/ExtendedMediator.cs
Tag: 25_16436701542
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 88
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.cctor()100%11100%
Publish(...)100%66100%
Publish(...)100%11100%
PublishBackground(...)100%11100%

File(s)

/home/runner/work/Pozitron.Extensions.MediatR/Pozitron.Extensions.MediatR/src/Pozitron.Extensions.MediatR/ExtendedMediator.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.Logging;
 3
 4namespace MediatR;
 5
 6/// <summary>
 7/// Extended mediator implementation.
 8/// </summary>
 9/// <param name="serviceScopeFactory"></param>
 10/// <param name="serviceProvider"></param>
 11public class ExtendedMediator(
 12    IServiceScopeFactory serviceScopeFactory,
 13    IServiceProvider serviceProvider)
 1514    : Mediator(serviceProvider)
 15{
 1516    private readonly IServiceScopeFactory _serviceScopeFactory = serviceScopeFactory;
 1517    private readonly IServiceProvider _serviceProvider = serviceProvider;
 18
 119    private static readonly Dictionary<int, INotificationPublisher> _publishers = new()
 120    {
 121        [(int)PublishStrategy.Sequential] = new SequentialPublisher(),
 122        [(int)PublishStrategy.SequentialAll] = new SequentialAllPublisher(),
 123        [(int)PublishStrategy.WhenAll] = new WhenAllPublisher(),
 124    };
 25
 26    /// <summary>
 27    /// Asynchronously send a notification to multiple handlers using the specified strategy.
 28    /// </summary>
 29    /// <typeparam name="TNotification"></typeparam>
 30    /// <param name="notification">Notification object</param>
 31    /// <param name="strategy">Publish strategy</param>
 32    /// <param name="cancellationToken">Optional cancellation token</param>
 33    /// <returns>A task that represents the publish operation.</returns>
 34    public Task Publish<TNotification>(
 35        TNotification notification,
 36        PublishStrategy strategy,
 37        CancellationToken cancellationToken = default)
 38        where TNotification : INotification
 39    {
 1540        var isBackgroundTask = (int)strategy > 10;
 1541        var key = isBackgroundTask
 1542            ? (int)strategy - 10
 1543            : (int)strategy;
 44
 1545        if (_publishers.TryGetValue(key, out var publisher))
 46        {
 1447            return isBackgroundTask
 1448                ? PublishBackground(_serviceScopeFactory, notification, publisher, cancellationToken)
 1449                : Publish(_serviceProvider, notification, publisher, cancellationToken);
 50        }
 51
 152        return Publish(notification, cancellationToken);
 53    }
 54
 55    private static Task Publish<TNotification>(
 56        IServiceProvider serviceProvider,
 57        TNotification notification,
 58        INotificationPublisher publisher,
 59        CancellationToken cancellationToken) where TNotification : INotification
 1260        => new Mediator(serviceProvider, publisher).Publish(notification, cancellationToken);
 61
 62    private static Task PublishBackground<TNotification>(
 63        IServiceScopeFactory serviceScopeFactory,
 64        TNotification notification,
 65        INotificationPublisher publisher,
 66        CancellationToken cancellationToken) where TNotification : INotification
 67    {
 268        _ = Task.Run(async () =>
 269        {
 270            using var scope = serviceScopeFactory.CreateScope();
 271            var logger = scope.ServiceProvider.GetService<ILogger<ExtendedMediator>>();
 272
 273            try
 274            {
 275                var mediator = new Mediator(scope.ServiceProvider, publisher);
 276                await mediator.Publish(notification, cancellationToken).ConfigureAwait(false);
 177            }
 178            catch (Exception ex)
 279            {
 280                // The aggregate exceptions are already flattened by the publishers.
 181                logger?.LogError(ex, "Error occurred while executing the handler(s) in a background thread!");
 182            }
 283
 484        }, cancellationToken);
 85
 286        return Task.CompletedTask;
 87    }
 88}