| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Threading.Channels; |
| | | 3 | | using Elsa.Mediator.Channels; |
| | | 4 | | using Elsa.Mediator.Contracts; |
| | | 5 | | using Elsa.Mediator.HostedServices; |
| | | 6 | | using Elsa.Mediator.Middleware.Command; |
| | | 7 | | using Elsa.Mediator.Middleware.Command.Contracts; |
| | | 8 | | using Elsa.Mediator.Middleware.Notification; |
| | | 9 | | using Elsa.Mediator.Middleware.Notification.Contracts; |
| | | 10 | | using Elsa.Mediator.Middleware.Request; |
| | | 11 | | using Elsa.Mediator.Middleware.Request.Contracts; |
| | | 12 | | using Elsa.Mediator.Models; |
| | | 13 | | using Elsa.Mediator.Options; |
| | | 14 | | using Elsa.Mediator.Services; |
| | | 15 | | using JetBrains.Annotations; |
| | | 16 | | using Microsoft.Extensions.Logging; |
| | | 17 | | |
| | | 18 | | // ReSharper disable once CheckNamespace |
| | | 19 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Adds mediator services to the <see cref="IServiceCollection"/>. |
| | | 23 | | /// </summary> |
| | | 24 | | [PublicAPI] |
| | | 25 | | public 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 | | { |
| | 2 | 32 | | services.Configure(configure ?? (_ => { })); |
| | | 33 | | |
| | 1 | 34 | | return services |
| | 1 | 35 | | .AddScoped<IMediator, DefaultMediator>() |
| | 0 | 36 | | .AddScoped<IRequestSender>(sp => sp.GetRequiredService<IMediator>()) |
| | 320 | 37 | | .AddScoped<ICommandSender>(sp => sp.GetRequiredService<IMediator>()) |
| | 322 | 38 | | .AddScoped<INotificationSender>(sp => sp.GetRequiredService<IMediator>()) |
| | 1 | 39 | | .AddSingleton<IRequestPipeline, RequestPipeline>() |
| | 1 | 40 | | .AddSingleton<ICommandPipeline, CommandPipeline>() |
| | 1 | 41 | | .AddSingleton<INotificationPipeline, NotificationPipeline>() |
| | 1 | 42 | | ; |
| | | 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 | | { |
| | 1 | 52 | | return services |
| | 1 | 53 | | .AddSingleton<INotificationsChannel, NotificationsChannel>() |
| | 1 | 54 | | .AddSingleton<ICommandsChannel, CommandsChannel>() |
| | 1 | 55 | | .AddSingleton<IJobsChannel, JobsChannel>() |
| | 1 | 56 | | .AddSingleton<IJobQueue, JobQueue>() |
| | 1 | 57 | | .AddHostedService<JobRunnerHostedService>() |
| | 1 | 58 | | .AddHostedService<BackgroundCommandSenderHostedService>() |
| | 1 | 59 | | .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 | | { |
| | 0 | 67 | | services.AddSingleton<Channel<T>>(_ => CreateChannel<T>()); |
| | | 68 | | |
| | 0 | 69 | | services.AddHostedService(sp => |
| | 0 | 70 | | { |
| | 0 | 71 | | var channel = sp.GetRequiredService<Channel<T>>(); |
| | 0 | 72 | | var consumers = sp.GetServices<IConsumer<T>>(); |
| | 0 | 73 | | var logger = sp.GetRequiredService<ILogger<MessageProcessorHostedService<T>>>(); |
| | 0 | 74 | | return new MessageProcessorHostedService<T>(workerCount, channel, consumers, logger); |
| | 0 | 75 | | }); |
| | 0 | 76 | | 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 | | { |
| | 0 | 84 | | 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 |
| | 3 | 91 | | 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> => |
| | 0 | 98 | | 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 | | { |
| | 0 | 107 | | 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 | | { |
| | 24 | 116 | | 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 => |
| | 0 | 125 | | 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 => |
| | 0 | 133 | | 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 | | { |
| | 0 | 142 | | 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> |
| | 4 | 148 | | 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> |
| | 4 | 153 | | 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 | | { |
| | 4 | 160 | | return services |
| | 4 | 161 | | .AddNotificationHandlersFrom(assembly) |
| | 4 | 162 | | .AddRequestHandlersFrom(assembly) |
| | 4 | 163 | | .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> |
| | 6 | 169 | | 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> |
| | 0 | 174 | | 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> |
| | 4 | 179 | | 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> |
| | 0 | 184 | | 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> |
| | 0 | 189 | | 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> |
| | 4 | 194 | | 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> |
| | 0 | 199 | | 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> |
| | 0 | 204 | | 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> |
| | 4 | 209 | | public static IServiceCollection AddCommandHandlersFrom(this IServiceCollection services, Assembly assembly) => serv |
| | | 210 | | |
| | 0 | 211 | | private static IServiceCollection AddHandlersFromInternal<TService, TMarker>(this IServiceCollection services) => se |
| | 6 | 212 | | private static IServiceCollection AddHandlersFromInternal<TService>(this IServiceCollection services, Type assemblyM |
| | | 213 | | |
| | | 214 | | private static IServiceCollection AddHandlersFromInternal<TService>(this IServiceCollection services, Assembly assem |
| | | 215 | | { |
| | 18 | 216 | | var serviceType = typeof(TService); |
| | 3944 | 217 | | var types = assembly.DefinedTypes.Where(x => serviceType.IsAssignableFrom(x)); |
| | | 218 | | |
| | 82 | 219 | | foreach (var type in types) |
| | 23 | 220 | | services.AddScoped(serviceType, type); |
| | | 221 | | |
| | 18 | 222 | | return services; |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | private static Channel<T> CreateChannel<T>() => Channel.CreateUnbounded<T>(new UnboundedChannelOptions()); |
| | 0 | 226 | | private static ChannelReader<T> CreateChannelReader<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequi |
| | 0 | 227 | | private static ChannelWriter<T> CreateChannelWriter<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequi |
| | | 228 | | } |