One of the exciting new features of Glass V3 is the ability to auto map properties.

One of the original complaints of previous versions of Glass was the amount of code needed to configure it. Well in this version we have created the ability to automatically map properties. I have used the Sitecore version as an example but we will have the same in Umbraco.

To activate auto-mapping for attributes you just need to set the AutoMap property to true on SitecoreType attribute:

    [SitecoreType(AutoMap =  true)]
    public class HomePage
    {
        public virtual string Title { get; set; }
        public virtual string MainBody { get; set; }
    }

Or if you are using fluent configuration:

    var homePage = loader.Add().AutoMap();

It is that simple!

So how does it map? Most properties will automatically be mapped to a corresponding field name, so in our homepage class the property "Title" will be mapped to a Title field. However we also map special property names to specific metadata or relationships, in our extended homepage class below we map the following piece of data:

  • Id - mapped to the item's ID
  • Parent - mapped to the item's parent
  • Children - maps to the item's children
  • Url - maps to the item's Url
  • DisplayName - maps to the item's display name
    [SitecoreType(AutoMap =  true)]
    public class HomePage
    {
        public virtual string Title { get; set; }
        public virtual string MainBody { get; set; }
    
        public virtual Guid Id { get; set; }
        public virtual ContentPage Parent { get; set; }
        public virtual IEnumerable<ContentPage> Children { get; set; }
        public virtual string Url { get; set; }
        public virtual string DisplayName { get; set; }
    }

All the properties defined in the SitecoreInfoType enum are supported as auto-mapped properties.

You can also mix and match those elements that are auto-mapped and those that aren't. For example if I have an item where most of the fields match the properties but one field name has a space in it I can just specify the exception:

    [SitecoreType(AutoMap =  true)]
    public class Product
    {
        public virtual string Title { get; set; }
        public virtual string Description { get; set; }
        
        [SitecoreField("Product Cost")
        public virtual string Cost { get; set; }
    }

I think this is an exciting enhancement to Glass and should make development of Glass solutions much quicker.

Glass V3 isn't out just yet, we are just going through our last bit of testing before we make available for beta.

comments powered by Disqus