How to handle null values in the PipelineContext? #58
-
First, I think this project is awesome! I have been looking for something like that already for quite a while but the other solutions I have found so far just didn't feel compelling to me. I am still new to the library, but I can already see that it will surely fill my needs, thanks! I was wondering whether there is a good way of handling possible null values in the public sealed class MyPipeline(IPipelineStepFactory pipelineStepFactory)
{
public IPipeline<MyPipelineResult> Build()
{
var context = new MyPipelineContext();
var pipeline = new PipelineBuilder<MyPipelineContext, MyPipelineResult>(pipelineStepFactory, context)
.Add<DataLoadingStep>()
.Add<DataProcessingStep>()
.Build();
}
}
public sealed class DataLoadingStep(IPipelineStep<MyPipelineContext> nextStep) : IPipelineStep<MyPipelineContext>
{
public IPipelineStep<MyPipelineContext> NextStep { get; set; } = nextStep;
public async ValueTask ExecuteAsync(MyPipelineContext context, CancellationToken cancellationToken)
{
// perform data loading
context.Data = "loaded data";
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
public sealed class DataProcessingStep(IPipelineStep<MyPipelineContext> nextStep) : IPipelineStep<MyPipelineContext>
{
public IPipelineStep<MyPipelineContext> NextStep { get; set; } = nextStep;
public async ValueTask ExecuteAsync(MyPipelineContext context, CancellationToken cancellationToken)
{
// perform data processing using context.Data
context.Result = new MyPipelineResult(context.Data);
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
public sealed record MyPipelineResult(string Data);
public sealed class MyPipelineContext : PipelineContext<MyPipelineResult>
{
public string Data { get; set; } = null!;
public MyPipelineResult Result { get; set; } = null!;
public override MyPipelineResult GetPipelineResult()
{
return Result;
}
} With this solution, I need to be sure to properly set the Data property of the context, otherwise, I will get a However, it would be nice to get rid of this risk and to have compile-time checks to be sure that the values are properly set. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Thank you for your feedback! It's fantastic to hear that you're finding the library compelling for your needs! I hope I understand you right, you want to obtain only step-required data during step execution. Unfortunately, there is no way to do that. The context it's something that is shared between all pipeline steps.. you can try to do some workarounds to use different
|
Beta Was this translation helpful? Give feedback.
Thank you for sharing your insights and the code snippets!
I agree that decoupling the Pipeline and its steps offers more flexibility and scalability. Your suggestion of orchestrating the execution within the Pipeline itself rather than relying on each step to execute the next is an interesting approach.
Your idea of using source code generators sounds promising. However, I think that these changes could introduce a lot of breaking changes and require a lot of design/architecture changes. IMO this library solves the problems for which it was created.
Overall, your willingness to explore different approaches and suggest improvements is commendable. I'll consider your suggestions!