Sitecore with Glass Mapper, Fluent Configuration, and ImportMap

I figure most everyone who develops with Sitecore is using Glass Mapper, or at least has tried it out. First things first, I highly recommend the training course that was started as of Glass 5, which you can find at training.glass.lu. There’s a free introductory course, but if you want to get into the meat of what Glass can do, you’ll want to invest in the paid course (and you get a completion certificate to call yourself a certified developer to boot!).

One of the essential topics of Glass is model mapping from Sitecore to your POCOs. There are two methods, attribute configuration and fluent configuration. I’ve become a convert to fluent config, and this blog specifically talks about a feature you’ll need if you use inheritance. Of note, I didn’t see this feature talked about in the training, but it’s very important.

I was setting up a new project off Helixbase and setting up my models, using interfaces to do the trick. (A nice feature of Glass, you only need concrete models for creating new content.) I first set up the ubiquitous IGlassBase, representing a base template:

public interface IGlassBase {
  ID Id { get; set; }
}

Then I do the fluent configuration mapping:

public class GlassBaseMap : SitecoreGlassMap<IGlassBase> {
  public override void Configure() =>
    Map(config => {
      config.AutoMap();
      config.Id(f => f.Id);
    });
}

Then I set up a template that inherits from the base, let’s call it News. I want to represent this in my modeling, so I set up this interface:

public interface INews : IGlassBase {
  string Title { get; set; }
  string Description { get; set; }
  string Url { get; set; }

And the mapping:

public class NewsMap : SitecoreGlassMap<INews> {
  public override void Configure() =>
    Map(config => {
      config.AutoMap();
      config.Field(f => f.Title).FieldId("{25BED78C-4957-4165-998A-CA1B52F67497}");
      config.Field(f => f.Description).FieldId("{D9CF14B1-FA16-4BA6-9288-E8A174D4D522}");
      config.Info(f => f.Url).InfoType(SitecoreInfoType.Url);
    });
}

So this is looking good. Except when I ran this and filled the INews model, only the IGlassBase properties would fill. I know this had worked before, so I had to dig back into my old code for help, because I couldn’t find an answer online.

The key is to use the ImportMap call on the INews map. You’ll notice that the inheritance exists in the original interfaces, but not the mapping, and ImportMap provides this. All you need to do is modify the NewsMap class as follows:

public class NewsMap : SitecoreGlassMap<INews> {
  public override void Configure() =>
    Map(config => {
      ImportMap<IGlassBase>();
      config.AutoMap();
      config.Field(f => f.Title).FieldId("{25BED78C-4957-4165-998A-CA1B52F67497}");
      config.Field(f => f.Description).FieldId("{D9CF14B1-FA16-4BA6-9288-E8A174D4D522}");
      config.Info(f => f.Url).InfoType(SitecoreInfoType.Url);
    });
}

You only need to do this on the immediate inheritance, so if you have another model that inherits INews, in that new model’s mapping you would just need ImportMap on INews.

Doing this allows you to keep your Sitecore template inheritance setup and your model mapping in sync, which should make debugging and tracing through a lot easier to understand.

Spread the word. Share this post!

Leave a comment

Your email address will not be published. Required fields are marked *