Glass Mapper – Introducing Glass Maps

This is due to be released in an upcoming module (along with the delegate mapper described here), however – here is a sneak preview of what I have been working on for Glass Mapper.

I think Glass Mapper is a fantastic and versatile ORM for Sitecore, however one of my slight bugbears with Glass Mapper as it stands is this. I want to build my POCOs as isolated as they can be. Glass Mapper has provided a fluent API which is grand, but the implementation by default leaves it a little short in my humble opinion. Out of the box, it’s a bunch of configurations that you add to the GlassScCustom (or in your own) class. In addition to this, if you want to support inheritance – say IExtendedSitecoreItem : ISitecoreItem you have to define all of the mapping for ISitecoreItem on IExtendedSitecoreItem as well. There are ways in Glass to sort this – using imports and the like, but you can end up with the framework excepting if you import the same thing twice. For me, I wanted an interface similar to that of fluent nHibernate, using a mapping generic to provide the mappings in an isolation from the original POCOs so I have created just such a thing:

Fluent Mapping Generics

Lets consider a basic structure such as the one below.

    public interface ISitecoreItem
        string Name { get; set; }

        string Url { get; set; }

A short aside before I continue – Glass is great in that it gives us the ability to map to interfaces as well as concrete’s – please always map to interface as it better supports the multiple inheritance structure that makes Sitecore so versatile.

I am not going to describe how you would map this using fluent configuration as Michael and the team do a great job of that in their fluent configuration tutorial

So – I’ll continue and show you how my Glass Mapping structure maps this to Sitecore, please see the example below:

    public class SitecoreMap : SitecoreGlassMap<ISitecoreItem>
        public SitecoreMap(ISitecoreTypeProvider sitecoreTypeProvider) : base(sitecoreTypeProvider)

        public override void Configure()
                x => x.Info(y => y.Url).InfoType(SitecoreInfoType.Url),
                x => x.Info(y => y.Name).InfoType(SitecoreInfoType.Name));

This structure can be kept anywhere in your application, but in the solutions I have developed I tend to create a ‘Model’ project and a ‘Model.Mappings’ project to keep them isolated. This also has the added benefit of only having to include the Model project in your unit tests.

Now lets consider template inheritance – consider the structure below inherited from our ISitecoreItem.

    public interface IExtendedSitecoreItem : ISitecoreItem
        string Title { get; set; }

        int DelegateInteger { get; set; }

We can add a map for this with a class like the one below:

    public class ExtendedSitecoreMap : SitecoreGlassMap<IExtendedSitecoreItem>
        public ExtendedSitecoreMap(ISitecoreTypeProvider sitecoreTypeProvider)
            : base(sitecoreTypeProvider)

        public override void Configure()
            Map(x => ImportMap<ISitecoreItem>(),
                x => x.Field(y => y.Title),
                x => x.Delegate(y => y.DelegateInteger).GetValue(GetDelegateValue));

        private object GetDelegateValue(SitecoreDataMappingContext arg)
            return 10;

Note also – I have added the delegate mapping just to show this in situ.

Map Registration

In short, all of your Glass Maps can be added to the IoC container you use in a similar manner to the existing registrations. Some IoC containers come with assembly scanning functionality which helps this somewhat.

In our registration code we then need to use our container to give us back a fluent configuration loader which can be passed back to Glass. This configuration loader can then be passed to Glass in a similar manner to the tutorial above.

            ConfigurationMap configurationMap = new ConfigurationMap(mappingContainer);
            var configloader = configurationMap.GetConfigurationLoader<SitecoreFluentConfigurationLoader>();


With this mapping, we can (mostly) isolate our model classes and manage them more easily. This will be released at some point soon either as an open source module (I am open to it becoming a candidate for Glass itself also).

Other Reading

3 thoughts on “Glass Mapper – Introducing Glass Maps

  1. Pingback: Glass Mapper V4 – Mapping Generics | CardinalCore

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s