My two previous posts both contained implementations of custom processors for the httpRequestBegin pipeline.
Just a quick explanation about their structure and some absolute best practices when implementing your own Sitecore pipeline processors.
Check and exit fast
Since the httpRequestBegin pipeline handles all requests it is extremely important to check the context for validity before continuing processing. If a simple check fails then exit the processor right away.
Example
1 2 3 4 5 6 7 8 9 |
public class MyCustomProcessor { public void Process(PipelineArgs args) { if (!IsContextValidForProcessor()) return; DoSomeOperation(); } } |
This rule applies for all pipeline processors, if the context is not valid for the operation exit immediately and let the rest of the pipeline continue on.
Make least expensive checks first
One check might be more expensive than the others and to prevent it from being unnecessary executed in a context where the pipeline processor should not run anyway, always perform the least expensive checks first.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class MyCustomProcessor { public void Process(PipelineArgs args) { if (Context.Site == null) return; if (Context.Site.Name.Equals(Sitecore.Constants.ShellSiteName, StringComparison.InvariantCultureIgnoreCase)) return; var requiredValueFromItem = ReadSomeValueFromItem(); if (requiredValueFromItem == null) return; DoSomeOperation(); } } |
String comparison is cheaper than reading database values. The above example check is a very typical example checking if the current site context is the shell site.
That was it for this post.
Anders Laub Christoffersen
Anders has been working with Sitecore for over a decade and has in this time been the lead developer and architect on several large scale enterprise solutions all around the world. Anders was appointed the title of Sitecore Technical MVP in 2014 and has been re-appointed the title every year since then.
- Web |
- More Posts
One thought on “A note about Sitecore pipelines”