Fluent Configuration

In this tutorial we will look at how we can use fluent configuration. So far in the tutorials we have used attribute configuration but this can have some limitations in certain scenarios. For example imagine that you want to map call written by a third party because it is compiled you can not add attributes to it. Fluent configuration allows you to separate your configuration from the models you are using in you code. This is useful if are creating business logic that works across different CMS solutions.

For this tutorial we will be creating the events area of our website. The model for our events page is shown below, notice that it doesn't contain any attributes:

using System;
using Sitecore.Globalization;

namespace Glass.Mapper.Sites.Sc.Models.Content
{
    public class Event
    {
        public virtual Guid Id { get; set; }

        public virtual Language Language { get; set; }

        public virtual string Title { get; set; }

        public virtual string MainBody { get; set; }

        public virtual string Abstract { get; set; }

        public virtual string Location { get; set; }

        public virtual DateTime Start { get; set; }

        public virtual DateTime End { get; set; }

        public virtual string Url { get; set; }
    }
}

We now define the configuration in a separate class shown below, everything you can do with attribute configuration you can do with fluent configuration, it is just a matter of style. The example code below shows how to use auto mapping as well as how to map individual properties.

using Glass.Mapper.Sc.Configuration;
using Glass.Mapper.Sc.Configuration.Fluent;
using Glass.Mapper.Sites.Sc.Models.Content;

namespace Glass.Mapper.Sites.Sc.Models.Config
{
    public class ContentConfig
    {
        public static SitecoreFluentConfigurationLoader Load()
        {
            var loader = new SitecoreFluentConfigurationLoader();

            var eventConfig = loader.Add<Event>().AutoMap();
            eventConfig.Field(x => x.Title);
            eventConfig.Id(x => x.Id);
            eventConfig.Info(x => x.Language).InfoType(SitecoreInfoType.Language);

            return loader;
        }
    }
}

Now we have our configured class we need to tell Glass.Mapper to load the configuration, this is achieved by updating the Start method in the App_Start.GlassMapperSc class. We need to add to ensure that our configuration is returned as part of the IConfigurationLoader list:

using System.Collections.Generic;
using Castle.Windsor;
using Glass.Mapper.Configuration;
using Glass.Mapper.Sc.CastleWindsor;
using Glass.Mapper.Sc.Configuration.Attributes;
using Sitecore.SecurityModel;

namespace Glass.Mapper.Sites.Sc.App_Start
{

    public static class GlassMapperScCustom
    {
        public static void CastleConfig(IWindsorContainer container)
        {
            var config = new Config();
            container.Install(new SitecoreInstaller(config));
        }

        public static IConfigurationLoader[] GlassLoaders()
        {
          
            return new IConfigurationLoader[]
                {                   
                    Models.Config.ContentConfig.Load()
                };
        }
    }
}


There is no different in how you can use your classes once you have mapped them with either fluent or attribute configuration.