< Summary

Information
Class: Microsoft.Extensions.DependencyInjection.DependencyInjectionExtensions
Assembly: Elsa.Mediator
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Extensions/DependencyInjectionExtensions.cs
Line coverage
58%
Covered lines: 35
Uncovered lines: 25
Coverable lines: 60
Total lines: 228
Line coverage: 58.3%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Extensions/DependencyInjectionExtensions.cs

#LineLine coverage
 1using System.Reflection;
 2using System.Threading.Channels;
 3using Elsa.Mediator.Channels;
 4using Elsa.Mediator.Contracts;
 5using Elsa.Mediator.HostedServices;
 6using Elsa.Mediator.Middleware.Command;
 7using Elsa.Mediator.Middleware.Command.Contracts;
 8using Elsa.Mediator.Middleware.Notification;
 9using Elsa.Mediator.Middleware.Notification.Contracts;
 10using Elsa.Mediator.Middleware.Request;
 11using Elsa.Mediator.Middleware.Request.Contracts;
 12using Elsa.Mediator.Models;
 13using Elsa.Mediator.Options;
 14using Elsa.Mediator.Services;
 15using JetBrains.Annotations;
 16using Microsoft.Extensions.Logging;
 17
 18// ReSharper disable once CheckNamespace
 19namespace Microsoft.Extensions.DependencyInjection;
 20
 21/// <summary>
 22/// Adds mediator services to the <see cref="IServiceCollection"/>.
 23/// </summary>
 24[PublicAPI]
 25public static class DependencyInjectionExtensions
 26{
 27    /// <summary>
 28    /// Adds mediator services to the <see cref="IServiceCollection"/>.
 29    /// </summary>
 30    public static IServiceCollection AddMediator(this IServiceCollection services, Action<MediatorOptions>? configure = 
 31    {
 232        services.Configure(configure ?? (_ => { }));
 33
 134        return services
 135                .AddScoped<IMediator, DefaultMediator>()
 036                .AddScoped<IRequestSender>(sp => sp.GetRequiredService<IMediator>())
 32037                .AddScoped<ICommandSender>(sp => sp.GetRequiredService<IMediator>())
 32238                .AddScoped<INotificationSender>(sp => sp.GetRequiredService<IMediator>())
 139                .AddSingleton<IRequestPipeline, RequestPipeline>()
 140                .AddSingleton<ICommandPipeline, CommandPipeline>()
 141                .AddSingleton<INotificationPipeline, NotificationPipeline>()
 142            ;
 43    }
 44
 45    /// <summary>
 46    /// Adds mediator hosted services to the <see cref="IServiceCollection"/>.
 47    /// </summary>
 48    /// <param name="services"></param>
 49    /// <returns></returns>
 50    public static IServiceCollection AddMediatorHostedServices(this IServiceCollection services)
 51    {
 152        return services
 153            .AddSingleton<INotificationsChannel, NotificationsChannel>()
 154            .AddSingleton<ICommandsChannel, CommandsChannel>()
 155            .AddSingleton<IJobsChannel, JobsChannel>()
 156            .AddSingleton<IJobQueue, JobQueue>()
 157            .AddHostedService<JobRunnerHostedService>()
 158            .AddHostedService<BackgroundCommandSenderHostedService>()
 159            .AddHostedService<BackgroundEventPublisherHostedService>();
 60    }
 61
 62    /// <summary>
 63    /// Adds a <see cref="Channel{T}"/> to the <see cref="IServiceCollection"/> and a hosted service that continuously r
 64    /// </summary>
 65    public static IServiceCollection AddMessageChannel<T>(this IServiceCollection services, int workerCount = 1) where T
 66    {
 067        services.AddSingleton<Channel<T>>(_ => CreateChannel<T>());
 68
 069        services.AddHostedService(sp =>
 070        {
 071            var channel = sp.GetRequiredService<Channel<T>>();
 072            var consumers = sp.GetServices<IConsumer<T>>();
 073            var logger = sp.GetRequiredService<ILogger<MessageProcessorHostedService<T>>>();
 074            return new MessageProcessorHostedService<T>(workerCount, channel, consumers, logger);
 075        });
 076        return services;
 77    }
 78
 79    /// <summary>
 80    /// Adds a channel consumer.
 81    /// </summary>
 82    public static IServiceCollection AddMessageConsumer<T, TConsumer>(this IServiceCollection services) where TConsumer 
 83    {
 084        return services.AddScoped<IConsumer<T>, TConsumer>();
 85    }
 86
 87    /// <summary>
 88    /// Registers a <see cref="ICommandHandler{T}"/> with the service container.
 89    /// </summary>
 90    public static IServiceCollection AddCommandHandler<THandler>(this IServiceCollection services) where THandler : clas
 391        services.AddScoped<ICommandHandler, THandler>();
 92
 93    /// <summary>
 94    /// Registers a <see cref="ICommandHandler{T}"/> with the service container.
 95    /// </summary>
 96    public static IServiceCollection AddCommandHandler<THandler, TCommand>(this IServiceCollection services)
 97        where THandler : class, ICommandHandler<TCommand> where TCommand : ICommand<Unit> =>
 098        services.AddCommandHandler<THandler, TCommand, Unit>();
 99
 100    /// <summary>
 101    /// Registers a <see cref="ICommandHandler{T}"/> with the service container.
 102    /// </summary>
 103    public static IServiceCollection AddCommandHandler<THandler, TCommand, TResult>(this IServiceCollection services)
 104        where THandler : class, ICommandHandler<TCommand, TResult>
 105        where TCommand : ICommand<TResult>
 106    {
 0107        return services.AddScoped<ICommandHandler, THandler>();
 108    }
 109
 110    /// <summary>
 111    /// Registers a <see cref="ICommandHandler{T}"/> with the service container.
 112    /// </summary>
 113    public static IServiceCollection AddNotificationHandler<THandler>(this IServiceCollection services)
 114        where THandler : class, INotificationHandler
 115    {
 24116        return services.AddScoped<INotificationHandler, THandler>();
 117    }
 118
 119    /// <summary>
 120    /// Registers a <see cref="INotificationHandler{T}"/> with the service container.
 121    /// </summary>
 122    public static IServiceCollection AddNotificationHandler<THandler, TNotification>(this IServiceCollection services)
 123        where THandler : class, INotificationHandler<TNotification>
 124        where TNotification : INotification =>
 0125        services.AddScoped<INotificationHandler, THandler>();
 126
 127    /// <summary>
 128    /// Registers a <see cref="INotificationHandler{T}"/> with the service container.
 129    /// </summary>
 130    public static IServiceCollection AddNotificationHandler<THandler, TNotification>(this IServiceCollection services, F
 131        where THandler : class, INotificationHandler<TNotification>
 132        where TNotification : INotification =>
 0133        services.AddScoped<INotificationHandler, THandler>(factory);
 134
 135    /// <summary>
 136    /// Registers a <see cref="IRequestHandler{TRequest,TResponse}"/> with the service container.
 137    /// </summary>
 138    public static IServiceCollection AddRequestHandler<THandler, TRequest, TResponse>(this IServiceCollection services)
 139        where THandler : class, IRequestHandler<TRequest, TResponse>
 140        where TRequest : IRequest<TResponse>?
 141    {
 0142        return services.AddScoped<IRequestHandler, THandler>();
 143    }
 144
 145    /// <summary>
 146    /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container.
 147    /// </summary>
 4148    public static IServiceCollection AddHandlersFrom<TMarker>(this IServiceCollection services) => services.AddHandlersF
 149
 150    /// <summary>
 151    /// Registers all handlers from the assembly of the specified marker type with the service container.
 152    /// </summary>
 4153    public static IServiceCollection AddHandlersFrom(this IServiceCollection services, Type markerType) => services.AddH
 154
 155    /// <summary>
 156    /// Registers all handlers from the specified assembly with the service container.
 157    /// </summary>
 158    public static IServiceCollection AddHandlersFrom(this IServiceCollection services, Assembly assembly)
 159    {
 4160        return services
 4161            .AddNotificationHandlersFrom(assembly)
 4162            .AddRequestHandlersFrom(assembly)
 4163            .AddCommandHandlersFrom(assembly);
 164    }
 165
 166    /// <summary>
 167    /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container.
 168    /// </summary>
 6169    public static IServiceCollection AddNotificationHandlersFrom<TMarker>(this IServiceCollection services) => services.
 170
 171    /// <summary>
 172    /// Registers all handlers from the assembly of the specified marker type with the service container.
 173    /// </summary>
 0174    public static IServiceCollection AddNotificationHandlersFrom(this IServiceCollection services, Type markerType) => s
 175
 176    /// <summary>
 177    /// Registers all handlers from the specified assembly with the service container.
 178    /// </summary>
 4179    public static IServiceCollection AddNotificationHandlersFrom(this IServiceCollection services, Assembly assembly) =>
 180
 181    /// <summary>
 182    /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container.
 183    /// </summary>
 0184    public static IServiceCollection AddRequestHandlersFrom<TMarker>(this IServiceCollection services) => services.AddHa
 185
 186    /// <summary>
 187    /// Registers all handlers from the assembly of the specified marker type with the service container.
 188    /// </summary>
 0189    public static IServiceCollection AddRequestHandlersFrom(this IServiceCollection services, Type markerType) => servic
 190
 191    /// <summary>
 192    /// Registers all handlers from the specified assembly with the service container.
 193    /// </summary>
 4194    public static IServiceCollection AddRequestHandlersFrom(this IServiceCollection services, Assembly assembly) => serv
 195
 196    /// <summary>
 197    /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container.
 198    /// </summary>
 0199    public static IServiceCollection AddCommandHandlersFrom<TMarker>(this IServiceCollection services) => services.AddHa
 200
 201    /// <summary>
 202    /// Registers all handlers from the assembly of the specified marker type with the service container.
 203    /// </summary>
 0204    public static IServiceCollection AddCommandHandlersFrom(this IServiceCollection services, Type markerType) => servic
 205
 206    /// <summary>
 207    /// Registers all handlers from the specified assembly with the service container.
 208    /// </summary>
 4209    public static IServiceCollection AddCommandHandlersFrom(this IServiceCollection services, Assembly assembly) => serv
 210
 0211    private static IServiceCollection AddHandlersFromInternal<TService, TMarker>(this IServiceCollection services) => se
 6212    private static IServiceCollection AddHandlersFromInternal<TService>(this IServiceCollection services, Type assemblyM
 213
 214    private static IServiceCollection AddHandlersFromInternal<TService>(this IServiceCollection services, Assembly assem
 215    {
 18216        var serviceType = typeof(TService);
 3944217        var types = assembly.DefinedTypes.Where(x => serviceType.IsAssignableFrom(x));
 218
 82219        foreach (var type in types)
 23220            services.AddScoped(serviceType, type);
 221
 18222        return services;
 223    }
 224
 0225    private static Channel<T> CreateChannel<T>() => Channel.CreateUnbounded<T>(new UnboundedChannelOptions());
 0226    private static ChannelReader<T> CreateChannelReader<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequi
 0227    private static ChannelWriter<T> CreateChannelWriter<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequi
 228}

Methods/Properties

AddMediator(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action`1<Elsa.Mediator.Options.MediatorOptions>)
AddMediatorHostedServices(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddMessageChannel(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Int32)
AddMessageConsumer(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddCommandHandler(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddNotificationHandler(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddNotificationHandler(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Func`2<System.IServiceProvider,THandler>)
AddRequestHandler(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type)
AddHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly)
AddNotificationHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddNotificationHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type)
AddNotificationHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly)
AddRequestHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddRequestHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type)
AddRequestHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly)
AddCommandHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddCommandHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type)
AddCommandHandlersFrom(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly)
AddHandlersFromInternal(Microsoft.Extensions.DependencyInjection.IServiceCollection)
AddHandlersFromInternal(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Type)
AddHandlersFromInternal(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly)
CreateChannel()
CreateChannelReader(System.IServiceProvider)
CreateChannelWriter(System.IServiceProvider)