The latest release made some subtle but important changes to how attribute configuration is handled by Glass.Mapper. In the latest release we have simplified the process a lot so that you can write even less code.

No Need To Load

In previous versions of Glass.Mapper.Sc you will have seen these lines of code in the GlassMapperScCustom.cs file:

        public static IConfigurationLoader[] GlassLoaders(){
			var attributes = new SitecoreAttributeConfigurationLoader("YourAssembly");
			return new IConfigurationLoader[]{attributes};
        }

In the latest version the SitecoreAttributeConfigurationLoader is no longer needed but you can use it to force pre-loading if you wish. The template itself no longer adds the attribute loader:

        public static IConfigurationLoader[] GlassLoaders(){
			return new IConfigurationLoader[]{};
        }

You should still add Fluent Configuration loaders here. With attribute configuration we now load them on demand when a type is requested for mapping.

Demo Setup

For this blog post I have setup the following item:


And I have the following model to represent this item:

    public class MyModel
    {
        public string Title { get; set; }
       
        public DateTime Date { get; set; }

        public string MenuTitle { get; set; }

        public string ItemUrl { get; set; }
    }

Everything is going to be rendered out on a simple sublayout which at the moment shows nothing:


No attributes

If we leave the model as it is:

    public class MyModel
    {
        public string Title { get; set; }
       
        public DateTime Date { get; set; }

        public string MenuTitle { get; set; }

        public string ItemUrl { get; set; }
    }

Only the Title and Date fields are mapped. The MenuTitle property and ItemUrl property don't match a Sitecore field or auto-mapping property, we can see this in the next screenshot:


Property Attributes

We can get MenuTitle and ItemUrl to map by adding attributes to both these properties. Notice that we do NOT need to add the SitecoreType attribute to the class any more:

    public class MyModel
    {
        public string Title { get; set; }
       
        public DateTime Date { get; set; }

        [SitecoreField("Menu Title")]
        public string MenuTitle { get; set; }

        [SitecoreInfo(SitecoreInfoType.Url)]
        public string ItemUrl { get; set; }
    }

Glass.Mapper will automatically map the first two properties and then use the attributes to map the last two properties:


Class Attribute

Finally you can still add the SitecoreType attribute class to the class if you need to specifiy the template ID or branch ID for creating items:

    [SitecoreType(TemplateId = "C62BA978-277D-4010-BC69-43D2E5F7DB5D", AutoMap = true)]
    public class MyModel
    {
        public string Title { get; set; }
       
        public DateTime Date { get; set; }

        [SitecoreField("Menu Title")]
        public string MenuTitle { get; set; }

        [SitecoreInfo(SitecoreInfoType.Url)]
        public string ItemUrl { get; set; }
    }
comments powered by Disqus