Sitecore pipelines
Sitecore addresses many application scenarios, such as receiving a request for a page, publishing an item or rendering the contents of a field, by implementing the scenario as a pipeline. Sitecore pipelines allow a single event to trigger the processing of some input by one or more discrete tasks.
Sitecore have made it extremely easy for developers to modify existing Sitecore pipelines and add custom pipelines of their own. When a scenario arises that can be broken down into discrete tasks and where the flexibility to allow reordering processing steps or the capability to add, remove and customize steps is required then a pipeline could be a suitable approach to implementing the scenario.
The pipes and filters pattern (aka. the pipeline pattern)
Conceptually, a Sitecore pipeline is based around the pipes and filters pattern which is also termed the pipeline pattern.
The basic elements of the pipe and filters pattern are:
- Pump : Pushes the data to be processed into the pipeline
- Pipe : The channel containing one or more sequential filters
- Filter : Performs the data processing (in Sitecore terms these are called Processors)
- Sink : Consumes the data processed by the pipeline
Advantages of the pipeline pattern
- Each processor in the pipeline can be treated as black boxes
- A pipeline can easily be made configurable
- Allows for re-use of processors could be used other pipelines
- Processor interaction is kept at a low level which keeps things simple
Disadvantages of the pipeline pattern
- Processors often force the data to be represented in the lowest common denominator, for example an item and an overhead may be introduce for parsing and unparsing objects.
- If a processor performs a long running operation it could lead to a bottle neck in the application make it less responsive
Working with Sitecore's pipelines
Pipelines are configured for use in Sitecore using configuration files. Sitecore's web.config file contains all the default pipeline configuration which is split up into two sections:
- /configuration/sitecore/pipelines
- /configuration/sitecore/processors
The pipelines section is used primarily to configure system processes and the processes section is primarily used to configure processes which are triggered by events in the Sitecore client and which may interact with a Sitecore user.
The two sections have slightly different ways to execute a pipeline:
- the CorePipeline is used to execute a pipeline configured in the pipelines section
- the Pipeline is used to execute a pipeline configured in the processor section
Sitecore itself uses many different pipelines and these are all configured in Sitecore's web.config file. This means it is a reasonably trivial task to modify many of Sitecore's default behaviors by simply adding to or modifying a the steps defined for a specific pipeline. Taking advantage of Sitecore's configuration file management features also ensures that all the config settings for your customisations can be separated into configurations files specific to your site or module. Of course care should be taken not to break any of the essential Sitecore functionality.
How to customize an existing pipeline
Customising an existing pipeline can be performed by creating a class containing a process method and modifiying the configuration of the pipeline, which should idealy be done using a custom include config file.
Adding a processor to an existing pipeline
- Create a new class which inherits from the PipelineArgs class
- Create a new class which has a method called Process and which accepts a parameter of the type of your new pipeline argument class
- Create a new .config file which adds your processor steps to the target pipeline
<
sitecore
>
<
pipelines
>
<
preprocessRequest
>
<
processor
patch:after
=
"*[@type='Sitecore.Pipelines.PreprocessRequest.StripLanguage, Sitecore.Kernel']"
type
=
"SitecoreCms.CodeSamples.Pipelines.SomeProcessor, SitecoreCms.CodeSamples"
/>
</
preprocessRequest
>
</
pipelines
>
</
sitecore
>
</
configuration
>
Remove a processor from a pipeline
<
sitecore
>
<
pipelines
>
<
preprocessRequest
>
<
processor
type
=
"SitecoreCms.CodeSamples.Pipelines.PreprocessRequest.SomeProcessor, SitecoreCms.CodeSamples"
>
<
patch:delete
/>
</
processor
>
</
preprocessRequest
>
</
pipelines
>
</
sitecore
>
</
configuration
>
Modify a processor in a pipeline
Create a custom include .config file in the Sitecore Include folder which modifies a specific processor
Custom pipelines
Creating a custom pipeline can be a suitable solution when requiring the flexibility to allow reordering of the processing steps performed by the application, or the capability to add, remove and customise steps. This is assuming that the processing required can easily be broken down into a set of discrete, independent steps.
How to create a custom pipeline
Pipeline processor arguments
- Create a new class which inherits from the PipelineArgs class
- Create a new class which has a method called Process and which accepts a parameter of the type of your new pipeline argument class
- Create a new .config file in the include folder which defines your custom pipeline
- Add your processor steps to the pipeline configuration
Executing the pipeline
CorePipeline
To call the pipeline we use the CorePipeline.Run method as follows:
var myPipelineArgs = new PipelineArgs();
CorePipeline.Run("myPipeline",myPipelineA);
Log.Info(args.Result, this);
Pipeline
When interacting with a user via Sitecore's client, pipelines are defined in the processors section of configuration. In order to gain access to the pipelines defined in this section the Pipeline class is used.
Pipeline.Start("myProcess", new MyProcessArgs());
Design Pattern References:
Pipes and filters pattern @ MSDN
Simple Pipe and Filters Implementation in C# with Fluent Interface Behavior