TIHIDI: Glass Mapper with Custom Models and Mapping using Fluent Configuration

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; }
    }
}

using Glass.Mapper.Sc.Configuration;
using Glass.Mapper.Sc.Maps;
using TIHIDI.Models;

namespace TIHIDI.Mapping
{
    public class GlassBaseMap : SitecoreGlassMap
    {

        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.

custommodel2

namespace TIHIDI.Models.Templates.Data
{
    public interface ISampleStatus
    {
        string Status { get; set; }
        string StatusDescription { get; set; }
    }
}
        public class SampleStatusMap : SitecoreGlassMap
    {
        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();
            });
        }
    }

custommodel1

namespace TIHIDI.Models.Templates.Data
{
    public interface ICommunityType
    {
        string CommunityType { get; set; }
        string TypeDescription { get; set; }
        bool Inactive { get; set; }
    }
}

            public class CommunityTypeMap : SitecoreGlassMap
    {
        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();
            });
        }
    }

custommodel4

namespace TIHIDI.Models.Templates.Data
{
    public interface ICommunityChildren
    {
        string DummyContent { get; set; }
    }
}
        public class CommunityChildrenMap : SitecoreGlassMap
    {
        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();
            });
        }
    }

custommodel3

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 CommunityType { get; set; }
        Link CommunitySite { get; set; }
        ISampleStatus CommunityStatus { get; set; }

        IEnumerable MyChildren { get; set; }

        IEnumerable 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
    {

        public override void Configure()
        {
            ITIHIDISettings settings = TIHIDISettings.Current;

            Map(x =>
            {
                ImportMap(); //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).

2 thoughts on “TIHIDI: Glass Mapper with Custom Models and Mapping using Fluent Configuration”

  1. Don’t you need to define the fluent api mappings in the glassmappersc.cs file in order to tell glass what mappings to use? I didn’t see that included in this?

    Also I assume with the use of a fluent api, that you aren’t generating glass models using tds and their t4 templates? Is that because you don’t have tds or more that you prefer using the fluent api and/or customizing your poco models yourself?

Leave a Reply

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