| | | 1 | | using Elsa.Mediator.Middleware.Notification.Contracts; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Mediator.Middleware.Notification; |
| | | 4 | | |
| | | 5 | | /// <inheritdoc /> |
| | | 6 | | public class NotificationPipelineBuilder : INotificationPipelineBuilder |
| | | 7 | | { |
| | | 8 | | private const string ServicesKey = "mediator.Services"; |
| | 1 | 9 | | private readonly List<Func<NotificationMiddlewareDelegate, NotificationMiddlewareDelegate>> _components = new(); |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Initializes a new instance of the <see cref="NotificationPipelineBuilder"/> class. |
| | | 13 | | /// </summary> |
| | 1 | 14 | | public NotificationPipelineBuilder(IServiceProvider serviceProvider) |
| | | 15 | | { |
| | 1 | 16 | | ApplicationServices = serviceProvider; |
| | 1 | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | 3 | 20 | | public IDictionary<string, object?> Properties { get; } = new Dictionary<string, object?>(); |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public IServiceProvider ApplicationServices |
| | | 24 | | { |
| | 1 | 25 | | get => GetProperty<IServiceProvider>(ServicesKey)!; |
| | 1 | 26 | | set => SetProperty(ServicesKey, value); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public INotificationPipelineBuilder Use(Func<NotificationMiddlewareDelegate, NotificationMiddlewareDelegate> middlew |
| | | 31 | | { |
| | 1 | 32 | | _components.Add(middleware); |
| | 1 | 33 | | return this; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public NotificationMiddlewareDelegate Build() |
| | | 38 | | { |
| | 11317 | 39 | | NotificationMiddlewareDelegate pipeline = _ => new ValueTask(); |
| | | 40 | | |
| | 4 | 41 | | for (int i = _components.Count - 1; i >= 0; i--) |
| | | 42 | | { |
| | 1 | 43 | | pipeline = _components[i](pipeline); |
| | | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | return pipeline; |
| | | 47 | | } |
| | | 48 | | |
| | 1 | 49 | | private T? GetProperty<T>(string key) => Properties.TryGetValue(key, out var value) ? (T?)value : default(T); |
| | 1 | 50 | | private void SetProperty<T>(string key, T value) => Properties[key] = value; |
| | | 51 | | } |