Search code examples
c#sitecoreglass-mapper

Using fluent mapping on descendant types with glass mapper


I have a type that I'm mapping (FooterFolder) which has a property of SocialMedia which I want to map using its own fluent configuration.

Here's the configuration of the Footer

loader.Add<FooterFolder>()
        .TemplateId("3E1C2A35-C62C-4FCE-B576-787F23E7728D")
        .AutoMap()
        .Fields(config =>
        {
            config.Field(x => x.LogoLink)
               .FieldName("Logo Link");
        })
        .Query(x => x.SocialLinks).IsRelative().InferType().Query("./Social Links/*");

And the SocialMedia

loader.Add<SocialMedia>()
        .TemplateId("1A1D6464-CB5A-433F-B7A9-9D10FA8E4BFE")
        .AutoMap()
        .Field(x => x.LinkLink).FieldName("Link");

If I change the name of the field on SocialMedia to just Link, it works (presumably using auto-mapping) but I'd like to have control over the mapping in case the name doesn't exactly match. Is there a way I can do this?

Here's my FooterFolder defenition:

public class FooterFolder : FolderBase
{
    public virtual string Copyright { get; set; }

    public virtual Image Logo { get; set; }

    public virtual Link LogoLink { get; set; }

    public virtual IEnumerable<SocialMedia> SocialLinks { get; set; }
}

And SocialMedia:

public class SocialMedia : ContentBase
{
    public virtual Image Icon { get; set; }

    public virtual Link LinkLink { get; set; }

    public virtual string Shorthand { get; set; }
}

(I'm naming the property LinkLink just to test the mapping. Normally they'd only be different for things like LogoLink where there's a space in the Sitecore definition)

In sitecore, the Social Media has a General Link named Link. If I change the name of the property on SocialMedia to Link it works just fine, through auto-mapping, presumably.


Solution

  • I ended up switching to using the SitecoreGlassMap version of configuration, rather than the IConfigurationLoader version, and it worked fine.