| | | 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 | | { |
| | 16 | 32 | | services.Configure(configure ?? (_ => { })); |
| | | 33 | | |
| | 8 | 34 | | return services |
| | 8 | 35 | | .AddScoped<IMediator, DefaultMediator>() |
| | 0 | 36 | | .AddScoped<IRequestSender>(sp => sp.GetRequiredService<IMediator>()) |
| | 405 | 37 | | .AddScoped<ICommandSender>(sp => sp.GetRequiredService<IMediator>()) |
| | 414 | 38 | | .AddScoped<INotificationSender>(sp => sp.GetRequiredService<IMediator>()) |
| | 8 | 39 | | .AddSingleton<IRequestPipeline, RequestPipeline>() |
| | 8 | 40 | | .AddSingleton<ICommandPipeline, CommandPipeline>() |
| | 8 | 41 | | .AddSingleton<INotificationPipeline, NotificationPipeline>() |
| | 8 | 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 | | { |
| | 3 | 52 | | return services |
| | 3 | 53 | | .AddMediatorBackgroundChannels() |
| | 3 | 54 | | .AddHostedService<JobRunnerHostedService>() |
| | 3 | 55 | | .AddHostedService<BackgroundCommandSenderHostedService>() |
| | 3 | 56 | | .AddHostedService<BackgroundEventPublisherHostedService>(); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Adds mediator background processing channels to the <see cref="IServiceCollection"/>. |
| | | 61 | | /// </summary> |
| | | 62 | | public static IServiceCollection AddMediatorBackgroundChannels(this IServiceCollection services) |
| | | 63 | | { |
| | 3 | 64 | | return services |
| | 3 | 65 | | .AddSingleton<INotificationsChannel, NotificationsChannel>() |
| | 3 | 66 | | .AddSingleton<ICommandsChannel, CommandsChannel>() |
| | 3 | 67 | | .AddSingleton<IJobsChannel, JobsChannel>() |
| | 3 | 68 | | .AddSingleton<IJobQueue, JobQueue>() |
| | 3 | 69 | | .AddSingleton<BackgroundCommandProcessor>() |
| | 3 | 70 | | .AddSingleton<BackgroundNotificationProcessor>() |
| | 3 | 71 | | .AddSingleton<BackgroundJobProcessor>() |
| | 3 | 72 | | ; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Adds a <see cref="Channel{T}"/> to the <see cref="IServiceCollection"/> and a hosted service that continuously r |
| | | 77 | | /// </summary> |
| | | 78 | | public static IServiceCollection AddMessageChannel<T>(this IServiceCollection services, int workerCount = 1) where T |
| | | 79 | | { |
| | 0 | 80 | | services.AddSingleton<Channel<T>>(_ => CreateChannel<T>()); |
| | | 81 | | |
| | 0 | 82 | | services.AddHostedService(sp => |
| | 0 | 83 | | { |
| | 0 | 84 | | var channel = sp.GetRequiredService<Channel<T>>(); |
| | 0 | 85 | | var consumers = sp.GetServices<IConsumer<T>>(); |
| | 0 | 86 | | var logger = sp.GetRequiredService<ILogger<MessageProcessorHostedService<T>>>(); |
| | 0 | 87 | | return new MessageProcessorHostedService<T>(workerCount, channel, consumers, logger); |
| | 0 | 88 | | }); |
| | 0 | 89 | | return services; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Adds a channel consumer. |
| | | 94 | | /// </summary> |
| | | 95 | | public static IServiceCollection AddMessageConsumer<T, TConsumer>(this IServiceCollection services) where TConsumer |
| | | 96 | | { |
| | 0 | 97 | | return services.AddScoped<IConsumer<T>, TConsumer>(); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Registers a <see cref="ICommandHandler{T}"/> with the service container. |
| | | 102 | | /// </summary> |
| | | 103 | | public static IServiceCollection AddCommandHandler<THandler>(this IServiceCollection services) where THandler : clas |
| | 14 | 104 | | services.AddScoped<ICommandHandler, THandler>(); |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Registers a <see cref="ICommandHandler{T}"/> with the service container. |
| | | 108 | | /// </summary> |
| | | 109 | | public static IServiceCollection AddCommandHandler<THandler, TCommand>(this IServiceCollection services) |
| | | 110 | | where THandler : class, ICommandHandler<TCommand> where TCommand : ICommand<Unit> => |
| | 0 | 111 | | services.AddCommandHandler<THandler, TCommand, Unit>(); |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Registers a <see cref="ICommandHandler{T}"/> with the service container. |
| | | 115 | | /// </summary> |
| | | 116 | | public static IServiceCollection AddCommandHandler<THandler, TCommand, TResult>(this IServiceCollection services) |
| | | 117 | | where THandler : class, ICommandHandler<TCommand, TResult> |
| | | 118 | | where TCommand : ICommand<TResult> |
| | | 119 | | { |
| | 0 | 120 | | return services.AddScoped<ICommandHandler, THandler>(); |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Registers a <see cref="ICommandHandler{T}"/> with the service container. |
| | | 125 | | /// </summary> |
| | | 126 | | public static IServiceCollection AddNotificationHandler<THandler>(this IServiceCollection services) |
| | | 127 | | where THandler : class, INotificationHandler |
| | | 128 | | { |
| | 75 | 129 | | return services.AddScoped<INotificationHandler, THandler>(); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Registers a <see cref="INotificationHandler{T}"/> with the service container. |
| | | 134 | | /// </summary> |
| | | 135 | | public static IServiceCollection AddNotificationHandler<THandler, TNotification>(this IServiceCollection services) |
| | | 136 | | where THandler : class, INotificationHandler<TNotification> |
| | | 137 | | where TNotification : INotification => |
| | 0 | 138 | | services.AddScoped<INotificationHandler, THandler>(); |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Registers a <see cref="INotificationHandler{T}"/> with the service container. |
| | | 142 | | /// </summary> |
| | | 143 | | public static IServiceCollection AddNotificationHandler<THandler, TNotification>(this IServiceCollection services, F |
| | | 144 | | where THandler : class, INotificationHandler<TNotification> |
| | | 145 | | where TNotification : INotification => |
| | 0 | 146 | | services.AddScoped<INotificationHandler, THandler>(factory); |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Registers a <see cref="IRequestHandler{TRequest,TResponse}"/> with the service container. |
| | | 150 | | /// </summary> |
| | | 151 | | public static IServiceCollection AddRequestHandler<THandler, TRequest, TResponse>(this IServiceCollection services) |
| | | 152 | | where THandler : class, IRequestHandler<TRequest, TResponse> |
| | | 153 | | where TRequest : IRequest<TResponse>? |
| | | 154 | | { |
| | 0 | 155 | | return services.AddScoped<IRequestHandler, THandler>(); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container. |
| | | 160 | | /// </summary> |
| | 12 | 161 | | public static IServiceCollection AddHandlersFrom<TMarker>(this IServiceCollection services) => services.AddHandlersF |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Registers all handlers from the assembly of the specified marker type with the service container. |
| | | 165 | | /// </summary> |
| | 12 | 166 | | public static IServiceCollection AddHandlersFrom(this IServiceCollection services, Type markerType) => services.AddH |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Registers all handlers from the specified assembly with the service container. |
| | | 170 | | /// </summary> |
| | | 171 | | public static IServiceCollection AddHandlersFrom(this IServiceCollection services, Assembly assembly) |
| | | 172 | | { |
| | 12 | 173 | | return services |
| | 12 | 174 | | .AddNotificationHandlersFrom(assembly) |
| | 12 | 175 | | .AddRequestHandlersFrom(assembly) |
| | 12 | 176 | | .AddCommandHandlersFrom(assembly); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container. |
| | | 181 | | /// </summary> |
| | 18 | 182 | | public static IServiceCollection AddNotificationHandlersFrom<TMarker>(this IServiceCollection services) => services. |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Registers all handlers from the assembly of the specified marker type with the service container. |
| | | 186 | | /// </summary> |
| | 0 | 187 | | public static IServiceCollection AddNotificationHandlersFrom(this IServiceCollection services, Type markerType) => s |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Registers all handlers from the specified assembly with the service container. |
| | | 191 | | /// </summary> |
| | 12 | 192 | | public static IServiceCollection AddNotificationHandlersFrom(this IServiceCollection services, Assembly assembly) => |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container. |
| | | 196 | | /// </summary> |
| | 0 | 197 | | public static IServiceCollection AddRequestHandlersFrom<TMarker>(this IServiceCollection services) => services.AddHa |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Registers all handlers from the assembly of the specified marker type with the service container. |
| | | 201 | | /// </summary> |
| | 0 | 202 | | public static IServiceCollection AddRequestHandlersFrom(this IServiceCollection services, Type markerType) => servic |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// Registers all handlers from the specified assembly with the service container. |
| | | 206 | | /// </summary> |
| | 12 | 207 | | public static IServiceCollection AddRequestHandlersFrom(this IServiceCollection services, Assembly assembly) => serv |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Registers all handlers from the assembly of the specified <c>TMarker</c> type with the service container. |
| | | 211 | | /// </summary> |
| | 0 | 212 | | public static IServiceCollection AddCommandHandlersFrom<TMarker>(this IServiceCollection services) => services.AddHa |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Registers all handlers from the assembly of the specified marker type with the service container. |
| | | 216 | | /// </summary> |
| | 0 | 217 | | public static IServiceCollection AddCommandHandlersFrom(this IServiceCollection services, Type markerType) => servic |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Registers all handlers from the specified assembly with the service container. |
| | | 221 | | /// </summary> |
| | 12 | 222 | | public static IServiceCollection AddCommandHandlersFrom(this IServiceCollection services, Assembly assembly) => serv |
| | | 223 | | |
| | 0 | 224 | | private static IServiceCollection AddHandlersFromInternal<TService, TMarker>(this IServiceCollection services) => se |
| | 18 | 225 | | private static IServiceCollection AddHandlersFromInternal<TService>(this IServiceCollection services, Type assemblyM |
| | | 226 | | |
| | | 227 | | private static IServiceCollection AddHandlersFromInternal<TService>(this IServiceCollection services, Assembly assem |
| | | 228 | | { |
| | 54 | 229 | | var serviceType = typeof(TService); |
| | 13518 | 230 | | var types = assembly.DefinedTypes.Where(x => serviceType.IsAssignableFrom(x)); |
| | | 231 | | |
| | 246 | 232 | | foreach (var type in types) |
| | 69 | 233 | | services.AddScoped(serviceType, type); |
| | | 234 | | |
| | 54 | 235 | | return services; |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | private static Channel<T> CreateChannel<T>() => Channel.CreateUnbounded<T>(new UnboundedChannelOptions()); |
| | 0 | 239 | | private static ChannelReader<T> CreateChannelReader<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequi |
| | 0 | 240 | | private static ChannelWriter<T> CreateChannelWriter<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequi |
| | | 241 | | } |