In my previous post, I used Glass.Sitecore.Mapper to grab content out of Sitecore for use in expanding tokens set in Standard Values fields.
While writing the code for that article, I recalled Alex Shyba asking whether it were possible to move Glass initialization code out of the Global.asax and into an initialize pipeline — Mike Edwards, the developer of Glass, illustrates how one initializes Glass in the Global.asax on github.
I do remember Mike saying it were possible, although I am uncertain whether anyone has done this.
As a follow up to Alex’s tweet, I’ve decided to do just that — create an initialize pipeline that will load up Glass models. I’ve also moved the model namespaces and assembly definitions into a patch config file along with defining the initialize pipeline.
Here is the pipeline I’ve written to do this:
using System.Collections.Generic;
using System.Linq;
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Sitecore.Pipelines;
using Glass.Sitecore.Mapper.Configuration.Attributes;
namespace Sitecore.Sandbox.Pipelines.Loader
{
public class InitializeGlassMapper
{
public void Process(PipelineArgs args)
{
CreateContextIfApplicable(GetModelTypes());
}
private static void CreateContextIfApplicable(IEnumerable<string> modelTypes)
{
if (CanCreateContext(modelTypes))
{
CreateContext(CreateNewAttributeConfigurationLoader(modelTypes));
}
}
private static bool CanCreateContext(IEnumerable<string> modelTypes)
{
return modelTypes != null && modelTypes.Count() > 0;
}
private static AttributeConfigurationLoader CreateNewAttributeConfigurationLoader(IEnumerable<string> modelTypes)
{
Assert.ArgumentNotNull(modelTypes, "modelTypes");
Assert.ArgumentCondition(modelTypes.Count() > 0, "modelTypes", "modelTypes collection must contain at least one string!");
return new AttributeConfigurationLoader(modelTypes.ToArray());
}
private static void CreateContext(AttributeConfigurationLoader loader)
{
Assert.ArgumentNotNull(loader, "loader");
Glass.Sitecore.Mapper.Context context = new Glass.Sitecore.Mapper.Context(loader);
}
private static IEnumerable<string> GetModelTypes()
{
return Factory.GetStringSet("glassMapperModels/type");
}
}
}
I’ve defined my new initialize pipeline in a patch config, coupled with Glass model namespace/assembly pairs:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<initialize>
<processor type="Sitecore.Sandbox.Pipelines.Loader.InitializeGlassMapper, Sitecore.Sandbox" />
</initialize>
</pipelines>
<glassMapperModels>
<type>Sitecore.Sandbox.Model, Sitecore.Sandbox</type>
</glassMapperModels>
</sitecore>
</configuration>
On the testing front, I validated what I developed for my previous post still works — it still works like a charm! 🙂




Mike, do you know which Sitecore code runs the initialize pipeline? Does it run on every page request or only once per application start? I was digging in the Sitecore Kernel yesterday and couldn’t find the pipeline run call that runs this pipeline.
According to this article http://laubplusco.net/global-asax-sitecore-pipelines/ by Anders Laub it runs on Application start.
Look in this namespace Sitecore.Web.Application