Posted 12th of April 2011 by Kern Herskind Nightingale

Inspired by the Compiled Domain Model and other code generation tools for Sitecore, I came up with a different approach to generating classes and eliminating magic strings.

In most cases the code generator uses the Sitecore API to traverse items in a Sitecore database to generate classes that represent templates and items. I cannot help feeling that basing the generated code on data from a database is a little detached from the codebase that lives in the versioning system.

For some time I have been using TDS to keep my items under version control (this could have been done using Sitecore's serialization feature - but TDS just makes it much more manageable). In a recent discussion with a client I had the idea of generating a model based on the serialized data from TDS.

This is a proof of concept using T4:

<#@ template hostspecific="true" language="C#" #>
<#@ assembly name="C:\Program Files (x86)\Hedgehog Development\Team Development for Sitecore (VS2010)\HedgehogDevelopment.SitecoreCommon.Data.dll" #>
<#@ assembly name="C:\Program Files (x86)\Hedgehog Development\Team Development for Sitecore (VS2010)\HedgehogDevelopment.SitecoreCommon.Data.Parser.dll" #>
<#@ import namespace="HedgehogDevelopment.SitecoreCommon.Data" #>
using System;   

<# 
SerializedTreeDataSource sitecoreDataSource = new SerializedTreeDataSource(this.Host.ResolvePath("..\\Herskind.Tds"));

foreach (var template in sitecoreDataSource.Templates)
{    
#>
public class <#= template.Name.Replace(" ", "") #>
{
<#
    foreach(var section in template.Sections)
    {
#>
    // Section: <#= section.Name #>
<#
        foreach(var field in section.OwnFields)
        {
#>
    // Field: <#= field.Name #> - Data type: <#= field.Fields["type"] #>
<#
        }
    }

    foreach(var baseitemid in template.BaseTemplateIds)
    {
#>
    // Base: <#= baseitemid.ToString() #>
<#
    }
#>
}
<#
}
#>

I realise that there is some way from this to fully automated model generation but I thought it was a fun demo.



blog comments powered by Disqus

About the author

Kern is a Sitecore Specialist with more than 10 years experince achitecting and developing Sitecore solutions. Kern was awarded Sitecore MVP in 2009, 2011 and again in 2015.
Currently Kern is helping the Sitecore Product team in shaping the future of the platform.

Kern Herskind Nightingale