diff --git a/src/PipeForge/DelegatePipelineStep.cs b/src/PipeForge/DelegatePipelineStep.cs new file mode 100644 index 0000000..e8cf8be --- /dev/null +++ b/src/PipeForge/DelegatePipelineStep.cs @@ -0,0 +1,24 @@ +namespace PipeForge; + +/// +/// A pipeline step that delegates execution to a provided function. +/// +/// The type of the pipeline context. +internal sealed class DelegatePipelineStep : PipelineStep +{ + private readonly Func, CancellationToken, Task> _invoke; + + /// + /// Initializes a new instance of the class with a delegate to execute. + /// + /// The delegate to invoke during pipeline execution. + public DelegatePipelineStep(Func, CancellationToken, Task> invoke) + { + _invoke = invoke ?? throw new ArgumentNullException(nameof(invoke)); + } + + public override Task InvokeAsync(TContext context, PipelineDelegate next, CancellationToken cancellationToken = default) + { + return _invoke(context, next, cancellationToken); + } +} diff --git a/src/PipeForge/Pipeline.cs b/src/PipeForge/Pipeline.cs index b2be89f..2d7550c 100644 --- a/src/PipeForge/Pipeline.cs +++ b/src/PipeForge/Pipeline.cs @@ -31,20 +31,6 @@ public static PipelineBuilder CreateFor() where T : class return new PipelineBuilder(); } - /// - /// Creates a new instance of for the specified context type. - /// - /// - /// This method is used to start building a pipeline for a specific type. - /// It allows for fluent configuration of pipeline steps. - /// - /// - /// - public static PipelineBuilder CreateFor(ILoggerFactory? loggerFactory) where T : class - { - return new PipelineBuilder(loggerFactory); - } - /// /// Discovers pipeline steps for a specific context type from all assemblies in the current AppDomain. /// diff --git a/src/PipeForge/PipelineBuilder.cs b/src/PipeForge/PipelineBuilder.cs index 510ca86..e352e7c 100644 --- a/src/PipeForge/PipelineBuilder.cs +++ b/src/PipeForge/PipelineBuilder.cs @@ -1,31 +1,38 @@ -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; namespace PipeForge; /// /// PipelineBuilder is used to configure and build a pipeline of steps. /// -/// -public class PipelineBuilder - where T : class +/// +public class PipelineBuilder + where TContext : class { - private readonly List>> _steps = new(); - private readonly ILoggerFactory? _loggerFactory; + private readonly IServiceCollection _services = new ServiceCollection(); - internal PipelineBuilder() { } + internal PipelineBuilder() + { } - internal PipelineBuilder(ILoggerFactory? loggerFactory) + /// + /// Builds a pipeline from the configured steps + /// + /// + public IPipelineRunner Build() { - _loggerFactory = loggerFactory; + //return new PipelineRunner(_services.BuildServiceProvider()); + throw new NotImplementedException(); } /// - /// Builds a pipeline from the configured steps + /// Configures the services used by the pipeline /// + /// /// - public IPipelineRunner Build() + public PipelineBuilder ConfigureServices(Action configure) { - return new PipelineRunner(_steps, _loggerFactory); + configure(_services); + return this; } /// @@ -33,21 +40,29 @@ public IPipelineRunner Build() /// /// /// - public PipelineBuilder WithStep() where TStep : IPipelineStep, new() + public PipelineBuilder WithStep() where TStep : class, IPipelineStep { - _steps.Add(new(() => new TStep())); + _services.AddPipelineStep(); return this; } /// - /// Adds a step to the pipeline + /// Adds a step to the pipeline using a delegate. By default, the step is registered with a transient lifetime. + /// You can specify a different lifetime if needed. /// - /// - /// + /// The delegate to invoke for the step. + /// The service lifetime for the step. Defaults to . /// - public PipelineBuilder WithStep(Func stepFactory) where TStep : IPipelineStep + public PipelineBuilder WithStep( + Func, CancellationToken, Task> invoke, + ServiceLifetime lifetime = ServiceLifetime.Transient) { - _steps.Add(new(() => stepFactory())); + var stepFactory = new Func>(_ => new DelegatePipelineStep(invoke)); + + _services.Add(ServiceDescriptor.Describe(typeof(IPipelineStep), stepFactory, lifetime)); + _services.Add(ServiceDescriptor.Describe(typeof(Lazy>), + sp => new Lazy>(() => stepFactory(sp)), lifetime)); + return this; } }