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
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
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.
Leave a Reply