Find out how you can automatically handle spaces in field names.

Yesterday on Slack the question of how to handle fields that contain a space came up on Slack. In this post we will look at the different ways we can handle this scenario.

By default with Glass you can create a property and if you do nothing else Glass will use the name of the property as the field name. For example the property:

public virtual string MyTitle { get; set;}

Will read from the field MyTitle:

This however means that all your field names must be valid C# names. If we want to use a field that has a space in the name then we need to add a little bit of configuration:

//attributes:

[SitecoreField("My New Field")]
public virtual string MyNewField {get; set;}

//fluent
config.Field(f => f.MyNewField).FieldName("My New Field")

This will solve the problem and everything works fine. However every keystroke I make is more time between me and my next coffee, so I want reduce the amount of code I must write. The problem with the above example is that I have to essentially write the field name twice, once for the property and once for the configuration.

What if we could get the system to do this automatically for us and save some typing? That would be pretty awesome.

With my properties I want to be able to use an underscore ("_") in my property name and have that replaced with a space at runtime. Therefore the field "My New Field" would be represented by the property:

public virtual string My_New_Field {get; set;}

Then I don't need to specify any configuration and my coffee is getting closer all the time.

To do this we need to extend a pipeline most people ignore which is the DataMapperResolver pipeline. This pipeline is responsible for loading the data mappers and configuring them for each property on your model. We need to extend the pipeline with an additional task that checks if the property that was configured was a field and then updates the field name by replacing the underscores:

    public class FieldsWithSpaceTask : IDataMapperResolverTask
    {
        public void Execute(DataMapperResolverArgs args)
        {
            if ( args.PropertyConfiguration is SitecoreFieldConfiguration)
            {
                var scConfig = args.PropertyConfiguration as SitecoreFieldConfiguration;

                scConfig.FieldName = scConfig.FieldName.Replace("_", " ");
            }
        }
    }

That was straight forward, so next we just need to tell Glass to use it. We can do this in the GlassMapperScCustom class that is added to your project:

		public static IDependencyResolver CreateResolver(){
			var config = new Glass.Mapper.Sc.Config();

			var dependencyResolver = new DependencyResolver(config);
                        dependencyResolver.DataMapperResolverFactory.Add(()=> new FieldsWithSpaceTask());
			
                        // add any changes to the standard resolver here
			return dependencyResolver;
		}

Notice that we add this to the end of the pipeline using the Add method. This is important because we want the standard Glass task to execute first and do most of the work.

With this change done we are now finished. Now every time I create a property that uses and underscore in the property name it will be converted to a space saving my fingers from having to do any more typing.

Glass needs your support!

Glass.Mappper.Sc is supported by the generous donations of the Glass.Mapper.Sc community! Their donations help fund the time, effort and hosting for the project.

These supporters are AMAZING and a huge thank you to all of you. You can find our list of supporters on the Rockstars page.

If you use Glass.Mapper.Sc and find it useful please consider supporting the project. Not only will you help the project but you could also get a discount on the Glass.Mapper.Sc training and join the Rockstars page.

Become a Rockstar