This blog post will go through how I setup Glass Mapper Models. I hand code models and also keep them close to a .NET Class/Interface. I setup a mapping project which will do the mapping of the custom model with Glass. I believe a cleaner and smaller View Model is the way to go. Some times you might need the entire template but most of the time its pieces of the template you need.

Visit http://www.glass.lu/mapper/sc for more information on Glass Mapper for Sitecore.

TIHIDI: Stands for This Is How I Do It. I am going to write a series of blog posts going through how I do Sitecore related work. Hope it helps you!

For all the fruit cakes who want to jump all over this, calm down. This is just a sample intended to showcase the mapping portion of it. This is not meant as a Sitecore or best practices example, although I am mindful of that.

Before I setup any models I setup an IGlassBase interface I use based on the need.

using Sitecore.Globalization; using System; using System.Collections.Generic; namespace TIHIDI.Models { public interface IGlassBase { Guid Id { get; set; } Language Language { get; set; } int Version { get; set; } IEnumerable BaseTemplateIds { get; set; } string TemplateName { get; set; } Guid TemplateId { get; set; } string Name { get; set; } string Url { get; set; } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 using Sitecore . Globalization ; using System ; using System . Collections . Generic ; namespace TIHIDI . Models { public interface IGlassBase { Guid Id { get ; set ; } Language Language { get ; set ; } int Version { get ; set ; } IEnumerable BaseTemplateIds { get ; set ; } string TemplateName { get ; set ; } Guid TemplateId { get ; set ; } string Name { get ; set ; } string Url { get ; set ; } } }

using Glass.Mapper.Sc.Configuration; using Glass.Mapper.Sc.Maps; using TIHIDI.Models; namespace TIHIDI.Mapping { public class GlassBaseMap : SitecoreGlassMap<IGlassBase> { public override void Configure() { Map(x => { x.AutoMap(); x.Info(y => y.BaseTemplateIds).InfoType(SitecoreInfoType.BaseTemplateIds); }); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 using Glass . Mapper . Sc . Configuration ; using Glass . Mapper . Sc . Maps ; using TIHIDI . Models ; namespace TIHIDI . Mapping { public class GlassBaseMap : SitecoreGlassMap <IGlassBase> { public override void Configure ( ) { Map ( x = > { x . AutoMap ( ) ; x . Info ( y = > y . BaseTemplateIds ) . InfoType ( SitecoreInfoType . BaseTemplateIds ) ; } ) ; } } }

Lets setup a few models and their mappings. For pure auto maps, I dump all auto maps in one file PureAutoMaps.cs.

namespace TIHIDI.Models.Templates.Data { public interface ISampleStatus { string Status { get; set; } string StatusDescription { get; set; } } } 1 2 3 4 5 6 7 8 9 namespace TIHIDI . Models . Templates . Data { public interface ISampleStatus { string Status { get ; set ; } string StatusDescription { get ; set ; } } }

public class SampleStatusMap : SitecoreGlassMap<ISampleStatus> { ITIHIDISettings settings = TIHIDISettings.Current; public override void Configure() { Map(x => { x.TemplateId(settings.SampleStatusTemplateId); //totally optional but you can use it to enforce template id x.AutoMap(); }); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class SampleStatusMap : SitecoreGlassMap <ISampleStatus> { ITIHIDISettings settings = TIHIDISettings . Current ; public override void Configure ( ) { Map ( x = > { x . TemplateId ( settings . SampleStatusTemplateId ) ; / / totally optional but you can use it to enforce template id x . AutoMap ( ) ; } ) ; } }

namespace TIHIDI.Models.Templates.Data { public interface ICommunityType { string CommunityType { get; set; } string TypeDescription { get; set; } bool Inactive { get; set; } } } 1 2 3 4 5 6 7 8 9 10 11 namespace TIHIDI . Models . Templates . Data { public interface ICommunityType { string CommunityType { get ; set ; } string TypeDescription { get ; set ; } bool Inactive { get ; set ; } } }

public class CommunityTypeMap : SitecoreGlassMap<ICommunityType> { ITIHIDISettings settings = TIHIDISettings.Current; public override void Configure() { Map(x => { x.TemplateId(settings.CommunityTypeTemplateId); //totally optional but you can use it to enforce template id x.AutoMap(); }); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class CommunityTypeMap : SitecoreGlassMap <ICommunityType> { ITIHIDISettings settings = TIHIDISettings . Current ; public override void Configure ( ) { Map ( x = > { x . TemplateId ( settings . CommunityTypeTemplateId ) ; / / totally optional but you can use it to enforce template id x . AutoMap ( ) ; } ) ; } }

namespace TIHIDI.Models.Templates.Data { public interface ICommunityChildren { string DummyContent { get; set; } } } 1 2 3 4 5 6 7 8 namespace TIHIDI . Models . Templates . Data { public interface ICommunityChildren { string DummyContent { get ; set ; } } }

public class CommunityChildrenMap : SitecoreGlassMap<ICommunityChildren> { ITIHIDISettings settings = TIHIDISettings.Current; public override void Configure() { Map(x => { x.TemplateId(settings.CommunityChildrenTemplateId); //totally optional but you can use it to enforce template id x.AutoMap(); }); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class CommunityChildrenMap : SitecoreGlassMap <ICommunityChildren> { ITIHIDISettings settings = TIHIDISettings . Current ; public override void Configure ( ) { Map ( x = > { x . TemplateId ( settings . CommunityChildrenTemplateId ) ; / / totally optional but you can use it to enforce template id x . AutoMap ( ) ; } ) ; } }

using Glass.Mapper.Sc.Fields; using System.Collections.Generic; namespace TIHIDI.Models.Templates.Data { public interface ISampleCommunity:IGlassBase { string CommunityName { get; set; } string CommunityDescription { get; set; } IEnumerable<ICommunityType> CommunityType { get; set; } Link CommunitySite { get; set; } ISampleStatus CommunityStatus { get; set; } IEnumerable<ICommunityChildren> MyChildren { get; set; } IEnumerable<ICommunityChildren> AlsoMyChildren { get; set; } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 using Glass . Mapper . Sc . Fields ; using System . Collections . Generic ; namespace TIHIDI . Models . Templates . Data { public interface ISampleCommunity : IGlassBase { string CommunityName { get ; set ; } string CommunityDescription { get ; set ; } IEnumerable <ICommunityType> CommunityType { get; set; } Link CommunitySite { get; set; } ISampleStatus CommunityStatus { get; set; } IEnumerable <ICommunityChildren> MyChildren { get; set; } IEnumerable <ICommunityChildren> AlsoMyChildren { get ; set ; } } }

using Glass.Mapper.Sc.Maps; using TIHIDI.Models; using TIHIDI.Models.Templates.Data; using TIHIDI.SCExtensions.Settings; namespace TIHIDI.Mapping.Templates.Data { public class SampleCommunityMap : SitecoreGlassMap<ISampleCommunity> { public override void Configure() { ITIHIDISettings settings = TIHIDISettings.Current; Map(x => { ImportMap<IGlassBase>(); //import existing maps //you can specify the file types so that Glass can interpret them correctly x.Field(y => y.CommunityType).FieldType(Glass.Mapper.Sc.Configuration.SitecoreFieldType.Multilist); x.Field(y => y.CommunityStatus).FieldType(Glass.Mapper.Sc.Configuration.SitecoreFieldType.Droplink); //as for the children we can do any of the following, each has its own advantages //you do not necessarily have to call the attribute children x.Query(y => y.MyChildren) .Query(string.Format("./*[@@templateid='{0}']", settings.CommunityChildrenTemplateId)) .IsRelative(); x.Children(y => y.AlsoMyChildren); x.TemplateId(settings.SampleCommunityTemplateId); //totally optional but you can use it to enforce template id x.AutoMap(); }); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 using Glass . Mapper . Sc . Maps ; using TIHIDI . Models ; using TIHIDI . Models . Templates . Data ; using TIHIDI . SCExtensions . Settings ; namespace TIHIDI . Mapping . Templates . Data { public class SampleCommunityMap : SitecoreGlassMap <ISampleCommunity> { public override void Configure ( ) { ITIHIDISettings settings = TIHIDISettings . Current ; Map ( x = > { ImportMap <IGlassBase> ( ) ; / / import existing maps / / you can specify the file types so that Glass can interpret them correctly x . Field ( y = > y . CommunityType ) . FieldType ( Glass . Mapper . Sc . Configuration . SitecoreFieldType . Multilist ) ; x . Field ( y = > y . CommunityStatus ) . FieldType ( Glass . Mapper . Sc . Configuration . SitecoreFieldType . Droplink ) ; / / as for the children we can do any of the following , each has its own advantages / / you do not necessarily have to call the attribute children x . Query ( y = > y . MyChildren ) . Query ( string . Format ( "./*[@@templateid='{0}']" , settings . CommunityChildrenTemplateId ) ) . IsRelative ( ) ; x . Children ( y = > y . AlsoMyChildren ) ; x . TemplateId ( settings . SampleCommunityTemplateId ) ; / / totally optional but you can use it to enforce template id x . AutoMap ( ) ; } ) ; } } }

If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).