| | | 1 | | using Elsa.Mediator.Middleware.Command.Contracts; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Mediator.Middleware.Command; |
| | | 4 | | |
| | | 5 | | /// <inheritdoc /> |
| | | 6 | | public class CommandPipelineBuilder : ICommandPipelineBuilder |
| | | 7 | | { |
| | | 8 | | private const string ServicesKey = "mediator.Services"; |
| | 1 | 9 | | private readonly List<Func<CommandMiddlewareDelegate, CommandMiddlewareDelegate>> _components = new(); |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Initializes a new instance of the <see cref="CommandPipelineBuilder"/> class. |
| | | 13 | | /// </summary> |
| | 1 | 14 | | public CommandPipelineBuilder(IServiceProvider serviceProvider) |
| | | 15 | | { |
| | 1 | 16 | | ApplicationServices = serviceProvider; |
| | 1 | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | 4 | 20 | | public IDictionary<string, object?> Properties { get; } = new Dictionary<string, object?>(); |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public IServiceProvider ApplicationServices |
| | | 24 | | { |
| | 2 | 25 | | get => GetProperty<IServiceProvider>(ServicesKey)!; |
| | 1 | 26 | | set => SetProperty(ServicesKey, value); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public ICommandPipelineBuilder Use(Func<CommandMiddlewareDelegate, CommandMiddlewareDelegate> middleware) |
| | | 31 | | { |
| | 2 | 32 | | _components.Add(middleware); |
| | 2 | 33 | | return this; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public ICommandPipelineBuilder Use(int index, Func<CommandMiddlewareDelegate, CommandMiddlewareDelegate> middleware) |
| | | 38 | | { |
| | 0 | 39 | | _components.Insert(index, middleware); |
| | 0 | 40 | | return this; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public ICommandPipelineBuilder Remove(Func<CommandMiddlewareDelegate, CommandMiddlewareDelegate> middleware) |
| | | 45 | | { |
| | 0 | 46 | | _components.Remove(middleware); |
| | 0 | 47 | | return this; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public ICommandPipelineBuilder RemoveAt(int index) |
| | | 52 | | { |
| | 0 | 53 | | _components.RemoveAt(index); |
| | 0 | 54 | | return this; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public ICommandPipelineBuilder Clear() |
| | | 59 | | { |
| | 0 | 60 | | _components.Clear(); |
| | 0 | 61 | | return this; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | | 65 | | public CommandMiddlewareDelegate Build() |
| | | 66 | | { |
| | 56 | 67 | | CommandMiddlewareDelegate pipeline = _ => new(); |
| | | 68 | | |
| | 6 | 69 | | for (var i = _components.Count - 1; i >= 0; i--) |
| | 2 | 70 | | pipeline = _components[i](pipeline); |
| | | 71 | | |
| | 1 | 72 | | return pipeline; |
| | | 73 | | } |
| | | 74 | | |
| | 2 | 75 | | private T? GetProperty<T>(string key) => Properties.TryGetValue(key, out var value) ? (T?)value : default; |
| | 1 | 76 | | private void SetProperty<T>(string key, T value) => Properties[key] = value; |
| | | 77 | | } |