All about Sitecore Commerce pipelines (Part 2 — Diving deeper)

Jack Spektor
4 min readOct 7, 2019

In previous story I wrote basic guide to Sitecore Commerce pipeline which gives a basic introduction into them and how to extend them.

Here i’m going to continue and cover a few more helpful tricks for Sitecore Commerce pipelines.

Creating a new pipeline

Creating a new pipeline involves three steps — interface class, pipeline class and registering a pipeline itself.

The interface class must derive from the IPipeline interface and defines pipeline input and output arguments. It also defines a context class.

We could also define pipeline display name which is helpful during debugging and viewing pipeline configuration, though its optional.

Custom pipeline interface example

Next we have to create a pipeline class which is very similar to pipeline interface but have to be derived from the CommercePipeline class instead.

Finally we have to register the pipeline in the ConfigureService method.

For this we call AddPipeline and pass the pipeline interface and class types as a parameter (see example).

We can also add pipeline blocks straight ahead in the same time.

Now the pipeline is registered and could be used in the application. We can extend it too as any other pipeline in other plugins provided they load after the pipeline registration.

Passing data between the blocks using context

Now imagine your pipeline requires to keep some temporary data in memory between block execution.

For this we could use the context class and its CommerceContext object:

Adding data to the context to reuse in other pipeline blocks

Now when data is in the context we can reuse it in next pipeline blocks execution.

All we need is to get object from the context by passing its type:

Running another pipeline in the block/pipeline

Now imagine you need to run another pipeline from your pipeline block.

For this Sitecore Commerce provides CommerceCommander helper class. You could get an instance of the class through construction injection in your pipeline block:

Then running pipeline anywhere in your block Run method is as easy as:

You can also even register another pipeline as a block, just be sure that pipeline definition fits block expected input/output arguments.

For that all is required is to pass the pipeline interface as a parameter to the block registration:

Aborting current pipeline execution

If pipeline needs to be aborted during its execution you can call context.Abort method that will cancel the pipeline execution.

For this you have to pass the reason for pipeline abortion in the parameters.

Note that this will result in error message appearing in the logs.

However there is a hidden trick to avoid logging any errors.

For that message has to start with Ok| string which will tell that pipeline abort is expected.

Pipeline error handling and progress reporting

If you want to handle exceptions in pipeline — its handy to know that its happening totally different from what you expect.

Simply wrapping the pipeline run code with try/catch doesnt work.

Instead you should provide additional handlers with the context options class instance.

onError — handles all exceptions that happens during the pipeline

onAbort — handles the abort if it happens during pipeline execution

onProgress — report on pipeline execution progress

Do you know any more tricks for pipelines? Happy to hear them in the comments section.

Happy pipelining!

--

--

Jack Spektor

Sitecore MVP 2018–2020, Sitecore Developer in AKQA, traveler and lover of life.