Sitecore Code Generation of Glass Models using serialized data with Sitecore.CodeGenerator

I was looking for a way to automatically generate Glass Models for my Sitecore Templates that had been serialized using Unicorn’s Rainbow YAML serialization formatter and came across this post  by Sitecore MVP Robin Hermanussen – http://hermanussen.eu/sitecore/wordpress/2015/04/generating-sitecore-code-without-tds

After reading the documents on GitHub it sounded perfect so I forked the repo and took a look at what the project was doing – https://github.com/hermanussen/sitecore.codegenerator

My first step were :-

  1. Updated to .Net4.6.1
  2. Used Visual Studio 2017 with T4 templates for 2017 – https://github.com/hagronnestad/T4Toolbox/releases/tag/vs2017-b1
  3. Used Sitecore Nuget Feed for Sitecore dlls.
  4. Targeting 8.2.170728 Sitecore 8.2 Update 5

Then I made some updates to the models that were being outputted using the T4 helpers from the tds-codegen project –  https://github.com/HedgehogDevelopment/tds-codegen

Changed to GlassGenerator :-

  1. Switched to Rainbow Provider
  2. Supported more Sitecore field types
  3. Changed file name to *.cs instead of *.gen.cs

Changes to GlassMappedClassTemplate :-

  1. Namespace uses the default namespace and the template location.
  2. Static Class for Template and Field Values
  3. Used static classes for SitecoreType TemplateId
  4. Used static classes for SitecoreField Name
  5. Added inheritance of IGlassBase interface

Here is the original output :-

// <autogenerated>
// This file was generated by T4 code generator SampleScriptTemplates.tt.
// Any changes made to this file manually will be lost next time the file is regenerated.
// </autogenerated>
namespace Sitecore.CodeGenerator.Sample.Glass.Models
{
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using global::Glass.Mapper.Sc.Configuration;
using global::Glass.Mapper.Sc.Configuration.Attributes;
using global::Glass.Mapper.Sc.Fields;
/// <summary>
/// Represents a mapped type for item {F4A0D5D5-49F9-488F-82AA-D4BE1ADCB662} in Sitecore.
/// Path: /sitecore/templates/Sitecore Code Generator Sample/Dog
/// </summary>
[SitecoreType(TemplateId = "{F4A0D5D5-49F9-488F-82AA-D4BE1ADCB662}")]
public partial interface IDog : IAnimal, INameable
{
#region Dog
/// <summary>
/// What food does the dog eat?
/// </summary>
[SitecoreField(FieldId = "{1033D7C1-9C1A-4C65-8316-81B6D5E46EB5}")]
IEnumerable<IFood> Eats { get; set; }
/// <summary>
/// Other dogs that this dog likes
/// </summary>
[SitecoreField(FieldId = "{20D7789D-BCE0-473E-BB46-59B216CE2C10}", Setting = SitecoreFieldSettings.DontLoadLazily)]
IEnumerable<IDog> Friends { get; set; }
#endregion
}
}

And this is what Sitecore.CodeGenerator is outputting now with my changes:-

// <autogenerated>
// This file was generated by T4 code generator SampleScriptTemplates.tt.
// Any changes made to this file manually will be lost next time the file is regenerated.
// </autogenerated>
namespace Sitecore.CodeGenerator.Sample.Glass.Models.sitecore.templates.SitecoreCodeGeneratorSample
{
using global::Sitecore.Data;
using global::System;
using global::System.Collections.Generic;
using global::System.Collections.Specialized;
using global::Glass.Mapper.Sc.Configuration.Attributes;
using global::Glass.Mapper.Sc.Configuration;
using global::Glass.Mapper.Sc.Fields;
/// <summary>
/// Represents a mapped type for item {F4A0D5D5-49F9-488F-82AA-D4BE1ADCB662} in Sitecore.
/// Path: /sitecore/templates/Sitecore Code Generator Sample/Dog
/// </summary>
[SitecoreType(TemplateId = IDogConstants.TemplateIdString, Cachable = true)]
public partial interface IDog: IGlassBase , global::Sitecore.CodeGenerator.Sample.Glass.Models.sitecore.templates.SitecoreCodeGeneratorSample.IAnimal, global::Sitecore.CodeGenerator.Sample.Glass.Models.sitecore.templates.SitecoreCodeGeneratorSample.INameable
{
#region Dog
/// <summary>
/// What food does the dog eat?
/// </summary>
[SitecoreField(IDogConstants.EatsFieldName)]
IEnumerable<IFood> Eats { get; set; }
/// <summary>
/// Other dogs that this dog likes
/// </summary>
[SitecoreField(IDogConstants.FriendsFieldName, Setting = SitecoreFieldSettings.DontLoadLazily)]
IEnumerable<IDog> Friends { get; set; }
#endregion
}
public static partial class IDogConstants
{
public const string TemplateIdString = "{F4A0D5D5-49F9-488F-82AA-D4BE1ADCB662}";
public static readonly ID TemplateId = new ID("{F4A0D5D5-49F9-488F-82AA-D4BE1ADCB662}");
public const string TemplateName = "Dog";
public static readonly ID EatsFieldId = new ID("{1033D7C1-9C1A-4C65-8316-81B6D5E46EB5}");
public const string EatsFieldName = "Eats";
public static readonly ID FriendsFieldId = new ID("{20D7789D-BCE0-473E-BB46-59B216CE2C10}");
public const string FriendsFieldName = "Friends";
}
}

 

Have a play with the code – https://github.com/Wesley-Lomax/sitecore.codegenerator and checkout Robins repo – https://github.com/hermanussen/sitecore.codegenerator I’ve found it a great timesaver for boiler plate models for your renderings.