|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | + |
| 4 | +namespace FluentPipelines.Async |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// A builder that builds an asynchronous pipeline with input and output data. |
| 8 | + /// </summary> |
| 9 | + /// <typeparam name="TIn">The type of data used as input to the pipeline.</typeparam> |
| 10 | + /// <typeparam name="TOut">The type of data returned from the pipeline.</typeparam> |
| 11 | + public class AsyncPipelineBuilder<TIn, TOut> : IAsyncPipelineBuilder<TIn, TOut> |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Gets the chain of delegates making up the steps of the pipeline. |
| 15 | + /// </summary> |
| 16 | + protected Func<TIn, Task<TOut>> Chain { get; } |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Initializes a new instance of the <see cref="AsyncPipelineBuilder{TIn, TOut}"/> class. |
| 20 | + /// </summary> |
| 21 | + /// <param name="chain">The chain of delegates making up the steps of the pipeline.</param> |
| 22 | + public AsyncPipelineBuilder(Func<TIn, Task<TOut>> chain) |
| 23 | + { |
| 24 | + Chain = chain ?? throw new ArgumentNullException(nameof(chain)); |
| 25 | + } |
| 26 | + |
| 27 | + /// <inheritdoc/> |
| 28 | + public virtual IAsyncPipeline<TIn, TOut> Build() |
| 29 | + { |
| 30 | + return new AsyncPipeline<TIn, TOut>(Chain); |
| 31 | + } |
| 32 | + |
| 33 | + /// <inheritdoc/> |
| 34 | + public virtual IAsyncPipelineBuilder<TIn, TNext> Then<TNext>(Func<TOut, TNext> step) |
| 35 | + { |
| 36 | + if(step is null) |
| 37 | + throw new ArgumentNullException(nameof(step)); |
| 38 | + |
| 39 | + return new AsyncPipelineBuilder<TIn, TNext>(async (input) => step(await Chain(input))); |
| 40 | + } |
| 41 | + |
| 42 | + /// <inheritdoc/> |
| 43 | + public virtual IAsyncPipelineBuilder<TIn, TNext> Then<TNext>(Func<TOut, Task<TNext>> step) |
| 44 | + { |
| 45 | + if(step is null) |
| 46 | + throw new ArgumentNullException(nameof(step)); |
| 47 | + |
| 48 | + return new AsyncPipelineBuilder<TIn, TNext>(async (input) => await step(await Chain(input))); |
| 49 | + } |
| 50 | + |
| 51 | + /// <inheritdoc/> |
| 52 | + public virtual IAsyncInPipelineBuilder<TIn> Then(Action<TOut> step) |
| 53 | + { |
| 54 | + if(step is null) |
| 55 | + throw new ArgumentNullException(nameof(step)); |
| 56 | + |
| 57 | + return new AsyncInPipelineBuilder<TIn>(async (input) => step(await Chain(input))); |
| 58 | + } |
| 59 | + |
| 60 | + /// <inheritdoc/> |
| 61 | + public virtual IAsyncInPipelineBuilder<TIn> Then(Func<TOut, Task> step) |
| 62 | + { |
| 63 | + if(step is null) |
| 64 | + throw new ArgumentNullException(nameof(step)); |
| 65 | + |
| 66 | + return new AsyncInPipelineBuilder<TIn>(async (input) => await step(await Chain(input))); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments