| | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | 2 | | using System.Reflection; |
| | 3 | |
|
| | 4 | | namespace MediatR; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// ExtendedMediator extensions. |
| | 8 | | /// DI extensions to scan for MediatR handlers and registers them. |
| | 9 | | /// </summary> |
| | 10 | | public static class MediatorExtensions |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Asynchronously send a notification to multiple handlers using the specified strategy. |
| | 14 | | /// </summary> |
| | 15 | | /// <typeparam name="TNotification"></typeparam> |
| | 16 | | /// <param name="publisher"></param> |
| | 17 | | /// <param name="notification">Notification object</param> |
| | 18 | | /// <param name="strategy">Publish strategy</param> |
| | 19 | | /// <param name="cancellationToken">Optional cancellation token</param> |
| | 20 | | /// <returns>A task that represents the publish operation.</returns> |
| | 21 | | /// <exception cref="NotSupportedException">Throws if the MediatorImplementationType is not configured to ExtendedMe |
| | 22 | | public static Task Publish<TNotification>( |
| | 23 | | this IPublisher publisher, |
| | 24 | | TNotification notification, |
| | 25 | | PublishStrategy strategy, |
| | 26 | | CancellationToken cancellationToken = default) |
| | 27 | | where TNotification : INotification |
| | 28 | | { |
| 16 | 29 | | return publisher is ExtendedMediator customMediator |
| 16 | 30 | | ? customMediator.Publish(notification, strategy, cancellationToken) |
| 16 | 31 | | : throw new NotSupportedException("The extended mediator implementation is not registered! Register it with |
| | 32 | | } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Registers handlers and mediator types. |
| | 36 | | /// The MediatorImplementationType is always set to ExtendedMediator. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="services">Service collection</param> |
| | 39 | | /// <param name="lifetime">Service lifetime to register services under</param> |
| | 40 | | /// <param name="types">Types from assemblies to scan</param> |
| | 41 | | /// <returns>Service collection</returns> |
| | 42 | | public static IServiceCollection AddExtendedMediatR( |
| | 43 | | this IServiceCollection services, |
| | 44 | | ServiceLifetime lifetime, |
| | 45 | | params Type[] types) |
| | 46 | | { |
| 2 | 47 | | var assemblies = types.Select(x => x.Assembly).ToArray(); |
| 1 | 48 | | return services.AddExtendedMediatR(lifetime, assemblies); |
| | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Registers handlers and mediator types. |
| | 53 | | /// The MediatorImplementationType is always set to ExtendedMediator. |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="services">Service collection</param> |
| | 56 | | /// <param name="types">Types from assemblies to scan</param> |
| | 57 | | /// <returns>Service collection</returns> |
| | 58 | | public static IServiceCollection AddExtendedMediatR( |
| | 59 | | this IServiceCollection services, |
| | 60 | | params Type[] types) |
| | 61 | | { |
| 14 | 62 | | var assemblies = types.Select(x => x.Assembly).ToArray(); |
| 7 | 63 | | return services.AddExtendedMediatR(assemblies); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Registers handlers and mediator types. |
| | 68 | | /// The MediatorImplementationType is always set to ExtendedMediator. |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="services">Service collection</param> |
| | 71 | | /// <param name="lifetime">Service lifetime to register services under</param> |
| | 72 | | /// <param name="assemblies">Assemblies to scan</param> |
| | 73 | | /// <returns>Service collection</returns> |
| | 74 | | public static IServiceCollection AddExtendedMediatR( |
| | 75 | | this IServiceCollection services, |
| | 76 | | ServiceLifetime lifetime, |
| | 77 | | params Assembly[] assemblies) |
| | 78 | | { |
| 2 | 79 | | var serviceConfig = new MediatRServiceConfiguration(); |
| 2 | 80 | | serviceConfig.Lifetime = lifetime; |
| 2 | 81 | | serviceConfig.RegisterServicesFromAssemblies(assemblies); |
| | 82 | |
|
| 2 | 83 | | return services.AddExtendedMediatR(serviceConfig); |
| | 84 | | } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Registers handlers and mediator types. |
| | 88 | | /// The MediatorImplementationType is always set to ExtendedMediator. |
| | 89 | | /// </summary> |
| | 90 | | /// <param name="services">Service collection</param> |
| | 91 | | /// <param name="assemblies">Assemblies to scan</param> |
| | 92 | | /// <returns>Service collection</returns> |
| | 93 | | public static IServiceCollection AddExtendedMediatR( |
| | 94 | | this IServiceCollection services, |
| | 95 | | params Assembly[] assemblies) |
| | 96 | | { |
| 7 | 97 | | var serviceConfig = new MediatRServiceConfiguration(); |
| 7 | 98 | | serviceConfig.RegisterServicesFromAssemblies(assemblies); |
| | 99 | |
|
| 7 | 100 | | return services.AddExtendedMediatR(serviceConfig); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Registers handlers and mediator types from the specified assemblies |
| | 105 | | /// The MediatorImplementationType is always set to ExtendedMediator. |
| | 106 | | /// </summary> |
| | 107 | | /// <param name="services">Service collection</param> |
| | 108 | | /// <param name="configuration">The action used to configure the options</param> |
| | 109 | | /// <returns>Service collection</returns> |
| | 110 | | public static IServiceCollection AddExtendedMediatR( |
| | 111 | | this IServiceCollection services, |
| | 112 | | Action<MediatRServiceConfiguration> configuration) |
| | 113 | | { |
| 11 | 114 | | var serviceConfig = new MediatRServiceConfiguration(); |
| 11 | 115 | | configuration.Invoke(serviceConfig); |
| | 116 | |
|
| 11 | 117 | | return services.AddExtendedMediatR(serviceConfig); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Registers handlers and mediator types from the specified assemblies. |
| | 122 | | /// The MediatorImplementationType is always set to ExtendedMediator. |
| | 123 | | /// </summary> |
| | 124 | | /// <param name="services">Service collection</param> |
| | 125 | | /// <param name="configuration">Configuration options</param> |
| | 126 | | /// <returns>Service collection</returns> |
| | 127 | | public static IServiceCollection AddExtendedMediatR( |
| | 128 | | this IServiceCollection services, |
| | 129 | | MediatRServiceConfiguration configuration) |
| | 130 | | { |
| 21 | 131 | | configuration.MediatorImplementationType = typeof(ExtendedMediator); |
| 21 | 132 | | services.AddMediatR(configuration); |
| | 133 | |
|
| 21 | 134 | | return services; |
| | 135 | | } |
| | 136 | | } |