Whenever I write a Sitecore pipeline processor, I put far too much effort in to restricting the circumstances under which the code should run. More often than not this includes preventing it from being invoked by Sitecore's internal sites (Shell, Admin, Login etc).
In a multi-site Sitecore instance things start to get a little muddled. Now, in addition to the internal sites, you also need to think about how your processor should behave for each of your public sites. All of this can lead to nasty looking code like the following:
This code is not very readable and would probably need quite a bit of fine tuning to get it right. Faulty code in a processor can completely break all of your sites, so the added complexity isn't ideal.
I wanted to create the means to remove the conditional logic introduced when managing multiple sites, and work with processors which are clear and focused in their purpose. So I wrote a simple "selective processor" class which acts as a wrapper for other processors, delegating it's responsibilities depending on the context site.
The web.config snippet below shows how this would work. We have a scenario where Sitecore needs to perform redirects using varying logic on several different sites. There are three processors, each with a discrete purpose - Site1Redirector, Site2Redirector, GenericRedirector.
The SelectiveProcessor contains the arbitrarily named grouping element cleverRedirectors. The hint="raw:Add" attribute instructs Sitecore to make use of any child elements of cleverRedirectors in SelectiveProcessor.
The redirector elements (also arbitrarily named) make the connection between a site and a processor. At runtime the Sitecore context site name is paired up with the value supplied in the site attribute. The class specified in the type attribute of the matched element is then instantiated.
Hopefully the the example above demonstrates how this approach provides flexibility in applying site-conditional logic while also keeping the purpose of a processor simple and clear:
- Sites 1 and 2 have their own custom processors.
- Site 3 does not have redirect processor at all.
- Sites 4 and 5 share a common processor.
Here's the code for the class:
For more information on how all this works read John West's blog posts on Pipelines and the Sitecore Configuration Factory.