|
1 | | -using Microsoft.Extensions.Logging; |
| 1 | +using Microsoft.Extensions.DependencyInjection; |
2 | 2 |
|
3 | 3 | namespace PipeForge; |
4 | 4 |
|
5 | 5 | /// <summary> |
6 | 6 | /// PipelineBuilder is used to configure and build a pipeline of steps. |
7 | 7 | /// </summary> |
8 | | -/// <typeparam name="T"></typeparam> |
9 | | -public class PipelineBuilder<T> |
10 | | - where T : class |
| 8 | +/// <typeparam name="TContext"></typeparam> |
| 9 | +public class PipelineBuilder<TContext> |
| 10 | + where TContext : class |
11 | 11 | { |
12 | | - private readonly List<Lazy<IPipelineStep<T>>> _steps = new(); |
13 | | - private readonly ILoggerFactory? _loggerFactory; |
| 12 | + private readonly IServiceCollection _services = new ServiceCollection(); |
14 | 13 |
|
15 | | - internal PipelineBuilder() { } |
| 14 | + internal PipelineBuilder() |
| 15 | + { } |
16 | 16 |
|
17 | | - internal PipelineBuilder(ILoggerFactory? loggerFactory) |
| 17 | + /// <summary> |
| 18 | + /// Builds a pipeline from the configured steps |
| 19 | + /// </summary> |
| 20 | + /// <returns></returns> |
| 21 | + public IPipelineRunner<TContext> Build() |
18 | 22 | { |
19 | | - _loggerFactory = loggerFactory; |
| 23 | + //return new PipelineRunner<TContext>(_services.BuildServiceProvider()); |
| 24 | + throw new NotImplementedException(); |
20 | 25 | } |
21 | 26 |
|
22 | 27 | /// <summary> |
23 | | - /// Builds a pipeline from the configured steps |
| 28 | + /// Configures the services used by the pipeline |
24 | 29 | /// </summary> |
| 30 | + /// <param name="configure"></param> |
25 | 31 | /// <returns></returns> |
26 | | - public IPipelineRunner<T> Build() |
| 32 | + public PipelineBuilder<TContext> ConfigureServices(Action<IServiceCollection> configure) |
27 | 33 | { |
28 | | - return new PipelineRunner<T>(_steps, _loggerFactory); |
| 34 | + configure(_services); |
| 35 | + return this; |
29 | 36 | } |
30 | 37 |
|
31 | 38 | /// <summary> |
32 | 39 | /// Adds a step to the pipeline |
33 | 40 | /// </summary> |
34 | 41 | /// <typeparam name="TStep"></typeparam> |
35 | 42 | /// <returns></returns> |
36 | | - public PipelineBuilder<T> WithStep<TStep>() where TStep : IPipelineStep<T>, new() |
| 43 | + public PipelineBuilder<TContext> WithStep<TStep>() where TStep : class, IPipelineStep<TContext> |
37 | 44 | { |
38 | | - _steps.Add(new(() => new TStep())); |
| 45 | + _services.AddPipelineStep<TStep>(); |
39 | 46 | return this; |
40 | 47 | } |
41 | 48 |
|
42 | 49 | /// <summary> |
43 | | - /// Adds a step to the pipeline |
| 50 | + /// Adds a step to the pipeline using a delegate. By default, the step is registered with a transient lifetime. |
| 51 | + /// You can specify a different lifetime if needed. |
44 | 52 | /// </summary> |
45 | | - /// <typeparam name="TStep"></typeparam> |
46 | | - /// <param name="stepFactory"></param> |
| 53 | + /// <param name="invoke">The delegate to invoke for the step.</param> |
| 54 | + /// <param name="lifetime">The service lifetime for the step. Defaults to <see cref="ServiceLifetime.Transient"/>.</param> |
47 | 55 | /// <returns></returns> |
48 | | - public PipelineBuilder<T> WithStep<TStep>(Func<TStep> stepFactory) where TStep : IPipelineStep<T> |
| 56 | + public PipelineBuilder<TContext> WithStep( |
| 57 | + Func<TContext, PipelineDelegate<TContext>, CancellationToken, Task> invoke, |
| 58 | + ServiceLifetime lifetime = ServiceLifetime.Transient) |
49 | 59 | { |
50 | | - _steps.Add(new(() => stepFactory())); |
| 60 | + var stepFactory = new Func<IServiceProvider, DelegatePipelineStep<TContext>>(_ => new DelegatePipelineStep<TContext>(invoke)); |
| 61 | + |
| 62 | + _services.Add(ServiceDescriptor.Describe(typeof(IPipelineStep<TContext>), stepFactory, lifetime)); |
| 63 | + _services.Add(ServiceDescriptor.Describe(typeof(Lazy<IPipelineStep<TContext>>), |
| 64 | + sp => new Lazy<IPipelineStep<TContext>>(() => stepFactory(sp)), lifetime)); |
| 65 | + |
51 | 66 | return this; |
52 | 67 | } |
53 | 68 | } |
0 commit comments