| | | 1 | | using Elsa.Mediator.Contracts; |
| | | 2 | | using Elsa.Mediator.Middleware.Command; |
| | | 3 | | using Elsa.Mediator.Middleware.Command.Contracts; |
| | | 4 | | using Elsa.Mediator.Middleware.Notification; |
| | | 5 | | using Elsa.Mediator.Middleware.Notification.Contracts; |
| | | 6 | | using Elsa.Mediator.Middleware.Request; |
| | | 7 | | using Elsa.Mediator.Middleware.Request.Contracts; |
| | | 8 | | using Elsa.Mediator.Models; |
| | | 9 | | using Elsa.Mediator.Options; |
| | | 10 | | using Microsoft.Extensions.Options; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Mediator.Services; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public class DefaultMediator : IMediator |
| | | 16 | | { |
| | | 17 | | private readonly IRequestPipeline _requestPipeline; |
| | | 18 | | private readonly ICommandPipeline _commandPipeline; |
| | | 19 | | private readonly INotificationPipeline _notificationPipeline; |
| | | 20 | | private readonly IServiceProvider _serviceProvider; |
| | | 21 | | private readonly IEventPublishingStrategy _defaultPublishingStrategy; |
| | | 22 | | private readonly ICommandStrategy _defaultCommandStrategy; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="DefaultMediator"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="requestPipeline">The request pipeline.</param> |
| | | 28 | | /// <param name="commandPipeline">The command pipeline.</param> |
| | | 29 | | /// <param name="notificationPipeline">The notification pipeline.</param> |
| | | 30 | | /// <param name="options">The mediator options.</param> |
| | | 31 | | /// <param name="serviceProvider">The service provider.</param> |
| | 322 | 32 | | public DefaultMediator( |
| | 322 | 33 | | IRequestPipeline requestPipeline, |
| | 322 | 34 | | ICommandPipeline commandPipeline, |
| | 322 | 35 | | INotificationPipeline notificationPipeline, |
| | 322 | 36 | | IOptions<MediatorOptions> options, |
| | 322 | 37 | | IServiceProvider serviceProvider) |
| | | 38 | | { |
| | 322 | 39 | | _requestPipeline = requestPipeline; |
| | 322 | 40 | | _commandPipeline = commandPipeline; |
| | 322 | 41 | | _notificationPipeline = notificationPipeline; |
| | 322 | 42 | | _serviceProvider = serviceProvider; |
| | 322 | 43 | | _defaultPublishingStrategy = options.Value.DefaultPublishingStrategy; |
| | 322 | 44 | | _defaultCommandStrategy = options.Value.DefaultCommandStrategy; |
| | 322 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | public async Task<T> SendAsync<T>(IRequest<T> request, CancellationToken cancellationToken = default) |
| | | 49 | | { |
| | 0 | 50 | | var responseType = typeof(T); |
| | 0 | 51 | | var context = new RequestContext(request, responseType, _serviceProvider, cancellationToken); |
| | 0 | 52 | | await _requestPipeline.ExecuteAsync(context); |
| | | 53 | | |
| | 0 | 54 | | return (T)context.Response; |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<T> SendAsync<T>(ICommand<T> command, ICommandStrategy? strategy, IDictionary<object, object> heade |
| | | 58 | | { |
| | 22 | 59 | | var resultType = typeof(T); |
| | 22 | 60 | | strategy ??= _defaultCommandStrategy; |
| | 22 | 61 | | var context = new CommandContext(command, strategy, resultType, headers, _serviceProvider, cancellationToken); |
| | 22 | 62 | | await _commandPipeline.InvokeAsync(context); |
| | | 63 | | |
| | 22 | 64 | | return (T)context.Result!; |
| | 22 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | 11 | 68 | | public async Task SendAsync(ICommand command, CancellationToken cancellationToken = default) => await SendAsync(comm |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | public Task SendAsync(ICommand command, ICommandStrategy? strategy = null, CancellationToken cancellationToken = def |
| | | 72 | | { |
| | 11 | 73 | | return SendAsync(command, strategy, new Dictionary<object, object>(), cancellationToken); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public async Task SendAsync(ICommand command, ICommandStrategy? strategy, IDictionary<object, object> headers, Cance |
| | | 77 | | { |
| | 33 | 78 | | var resultType = typeof(Unit); |
| | 33 | 79 | | strategy ??= _defaultCommandStrategy; |
| | 33 | 80 | | var context = new CommandContext(command, strategy, resultType, headers, _serviceProvider, cancellationToken); |
| | 33 | 81 | | await _commandPipeline.InvokeAsync(context); |
| | 33 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc /> |
| | | 85 | | public Task<T> SendAsync<T>(ICommand<T> command, CancellationToken cancellationToken = default) |
| | | 86 | | { |
| | 0 | 87 | | return SendAsync(command, new Dictionary<object, object>(), cancellationToken); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public Task<T> SendAsync<T>(ICommand<T> command, IDictionary<object, object> headers, CancellationToken cancellation |
| | | 91 | | { |
| | 0 | 92 | | return SendAsync(command, _defaultCommandStrategy, headers, cancellationToken); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <inheritdoc /> |
| | | 96 | | public Task<T> SendAsync<T>(ICommand<T> command, ICommandStrategy? strategy, CancellationToken cancellationToken = d |
| | | 97 | | { |
| | 0 | 98 | | return SendAsync(command, strategy, new Dictionary<object, object>(), cancellationToken); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <inheritdoc /> |
| | | 102 | | public async Task SendAsync(INotification notification, CancellationToken cancellationToken = default) |
| | | 103 | | { |
| | 11316 | 104 | | await SendAsync(notification, _defaultPublishingStrategy, cancellationToken); |
| | 11316 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <inheritdoc /> |
| | | 108 | | public async Task SendAsync(INotification notification, IEventPublishingStrategy? strategy = null, CancellationToken |
| | | 109 | | { |
| | 11316 | 110 | | strategy ??= _defaultPublishingStrategy; |
| | 11316 | 111 | | var context = new NotificationContext(notification, strategy, _serviceProvider, cancellationToken); |
| | 11316 | 112 | | await _notificationPipeline.ExecuteAsync(context); |
| | 11316 | 113 | | } |
| | | 114 | | } |