< Summary

Information
Class: NexMediator.Extensions.ServiceCollectionExtensions
Assembly: NexMediator.Extensions
File(s): /home/runner/work/NexMediator/NexMediator/src/NexMediator.Extensions/ServiceCollectionExtensions.cs
Line coverage
88%
Covered lines: 30
Uncovered lines: 4
Coverable lines: 34
Total lines: 64
Line coverage: 88.2%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddNexMediator(...)75%4488.23%

File(s)

/home/runner/work/NexMediator/NexMediator/src/NexMediator.Extensions/ServiceCollectionExtensions.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.DependencyInjection.Extensions;
 3using Microsoft.Extensions.Logging;
 4using NexMediator.Abstractions.Interfaces;
 5using NexMediator.Core;
 6using NexMediator.Pipeline.Behaviors;
 7
 8
 9namespace NexMediator.Extensions;
 10
 11/// <summary>
 12/// Extension method to register NexMediator and its dependencies
 13/// </summary>
 14public static class ServiceCollectionExtensions
 15{
 16    /// <summary>
 17    /// Registers all NexMediator services in the DI container
 18    /// </summary>
 19    /// <param name="services">The service collection to add to</param>
 20    /// <param name="configure">A delegate to configure pipeline behaviors</param>
 21    public static IServiceCollection AddNexMediator(this IServiceCollection services, Action<NexMediatorOptions>? config
 1822    {
 23        // Create configuration options with access to the service collection
 1824        var options = new NexMediatorOptions(services);
 1825        configure?.Invoke(options);
 26
 27        //Minimal logging infra for behaviors only
 1828        if (options.HasBehavior(typeof(LoggingBehavior<,>)))
 029        {
 030            services.TryAddSingleton<ILoggerFactory, LoggerFactory>();
 031            services.TryAddTransient(typeof(ILogger<>), typeof(Logger<>));
 032        }
 33
 34        // Register mediator core components
 1835        services.AddSingleton(options);
 1836        services.AddSingleton<INexMediator, DefaultNexMediator>();
 37
 38        // Automatically register request handlers, notification handlers, and validators
 3639        services.Scan(scan => scan.FromApplicationDependencies()
 3640
 3641            // Register all request handlers
 1842            .AddClasses(c => c.AssignableTo(typeof(INexRequestHandler<,>)))
 3643            .AsImplementedInterfaces()
 3644            .WithTransientLifetime()
 3645
 3646            // Register all notification handlers
 1847            .AddClasses(c => c.AssignableTo(typeof(INexNotificationHandler<>)))
 3648            .AsImplementedInterfaces()
 3649            .WithTransientLifetime()
 3650
 3651            // Register all stream handlers
 1852            .AddClasses(c => c.AssignableTo(typeof(INexStreamRequestHandler<,>)))
 3653            .AsImplementedInterfaces()
 3654            .WithTransientLifetime()
 3655
 3656            // Register all FluentValidation validators
 1857            .AddClasses(c => c.AssignableTo(typeof(FluentValidation.IValidator<>)))
 3658            .AsImplementedInterfaces()
 3659            .WithTransientLifetime()
 1860        );
 61
 1862        return services;
 1863    }
 64}