Skip to content

Sub pipeline receive a parameter from parent

Fernando Arias edited this page Jun 17, 2020 · 2 revisions

Imagine you have a pipeline with 3 steps. The second step is a sub-pipeline with three additional steps.

Example subpipeline

Now, let's add a requirement that involves steps 2.2 and 2.3 to get also the output from step 1 (i.e., the input of step 2).

Example subpipeline with args

Unfortunately, as steps are isolated, step 2.2 does only get input from 2.1, and therefore data from Step 1 (of type T1 in this case) is not available for Step 2.2.

We can solve this by using the additional interface IPipelineWithArgs, and its associated extension methods that support methods to accept additional parameters.

First, we need to modify Step2_2 and Step2_3 to accept parameters:

public class Step2_2 : IPipelineStepWithArgs<int, string, float>
{
    public float Process(int input, string req)
    {
        // Whatever...
    }

    public float Process(int input)
    {
        // throw an exception if we try to process this step without providing a value for 'req'
        throw new ArgumentNullException("req");
    }
}
var pipeline = new Pipeline<string, int>(req => req
  .AddStep(new Step1())
  .AddStep(new Pipeline<ConnectorRequest, ConnectorResponse>(input => input
    .AddStep(new Step2_1())
    .AddStep(new Step2_2(), req)
    .AddStep(new Step2_3()), req)
  )
  .AddStep(new Step3()));
Clone this wiki locally