In this blog post I am going to look you can work with Glass.Mapper.Sc in a Helix styled solution, specifically I will look at how to load configuration in a Helix solution

It is important to remember that Glass.Mapper.Sc provides multiple ways to load model configuration:

  • Attribute Loader
  • Fluent Loader
  • OnDemand

Generally you shouldn't use on-demand mapping in a Helix solution, it is safer load your configuration when your application starts. We will therefore focus on loading attribute and fluent configuration when the application starts.

Nuget packages

First we need to decide which nuget packages should be installed in each layer of the helix solution, this is shown in the image below:

In the foundation layer you should create a separate ORM project to contain the Glass.Mapper.Sc initialisation files contained in the Glass.Mapper.Sc.{version} nuget package. It is important that the Glass.Mapper.Sc.{version} nuget package is only referenced by a single module.

In the feature and project layers you must always reference Glass.Mapper.Sc.Core and optionally reference Glass.Mapper.Sc.Mvc if it is required.

Configuration Loading

Configuration can be loaded by using a new pipeline introduced in version 5.1 thanks to a contribution by Corey Smith.

To take advantage of this feature your Helix module must added a processor to the glassMapper.getGlassLoaders pipeline that inherits the GetGlassLoadersProcessor abstract class.

The processor below loads models from a module using attribute configuration:

public class FeatureGlassLoader : GetGlassLoadersProcessor {

    public void Process(GetGlassLoadersPipelineArgs args){

           var loader = new SitecoreAttributeConfigurationLoader("MyProject.Feature");
           args.Loaders.Add(loader); 
    }

}

In the module's Sitecore configuration the following  configuration is added:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <glassMapper.getGlassLoaders>
	<processor type="MyProject.Feature.FeatureGlassLoader , MyProject.Feature" />
      </glassMapper.getGlassLoaders>
    </pipelines>
  </sitecore>
</configuration>

Using this technique each module is responsible for loading it's own models without requiring any changes to the ORM module in the foundation layer.

Learn More

You can learn more about using Glass.Mapper.Sc in a helix solution in the the V5 Training Coursse, the lectures also cover model design and avoiding type hijacking as well as loads other information on how to use the framework.