Friday, September 23, 2016

TDS for Code Generation

Hi All,
Today we discuss about how generate code for templates from TDS via glass mapper.
At first thanks for this artice which make a clear picture about that link is here.

Configuring TDS with GlassMapper

Open up the TDS properties dialog. You can do this by right-clicking on your TDS project in solution explorer and select ‘Properties’ from the context menu.

Go to the ‘Code Generation’ tab. This is the area that you can configure TDS with Glassmapper. You will need to add in the following properties:
  • Enable Code Generation Checkbox
  • Select the target project (this is the website or class library you want the generated classes to live)
  • Add in the Folder, class name and .cs extension (be sure to use the right \ otherwise TDS will moan
  • Add in the namespace
After filling in these details, select ‘Save’. When you look within solution explorer in your TDS project you should now see a new folder called ‘Code Generation Templates’.


In here you need to add some custom Sitecore transformation files, known as T4 templates. These T4 templates can be thought of like the instruction manual to tell GlassMapper how to convert Sitecore data in C# classes. These files are the bit that create the ‘magic’ auto code generation. You can get the templates here. Download the following three files:
  • Helpers.tt
  • GlassV3Header.tt
  • GlassV3Item.tt
  • GeneralExtensions.tt
  • Inflector.tt
  • StringExtensions.tt
Do not use Github’s ‘Download It’ option. Add the templates and include them to your Visual Studio project file. Go back to the TDS properties -> Code Generation’ section. You now need to configure the ‘Header Transform File’ and the ‘Base Project Transform File’
Header Transform File -> GlassV3Header.tt
Base Project Transform File -> GlassV3Item.tt

When you save the project the file you specified in the target path should be generated with some code, mine looked like this:


namespace GlassMapperModels
{
    public partial interface IGlassBase{
         
        [SitecoreId]
        Guid Id{ get; }
        [SitecoreInfo(SitecoreInfoType.Language)]
        Language Language{ get; }
        [SitecoreInfo(SitecoreInfoType.Version)]
        int Version { get; }
    }
    public abstract partial class GlassBase : IGlassBase{
         
        [SitecoreId]
        public virtual Guid Id{ get; private set;}
        [SitecoreInfo(SitecoreInfoType.Language)]
        public virtual Language Language{ get; private set; }
        [SitecoreInfo(SitecoreInfoType.Version)]
        public virtual int Version { get; private set; }
        [SitecoreInfo(SitecoreInfoType.Url)]
        public virtual string Url { get; private set; }
    }
}

Note: Please have a look on below url if you faces any issue regarding t4 template.
https://www.teamdevelopmentforsitecore.com/Blog/t4-transform-file-problems-code-gen

I hope you enjoy this Sitecore blog. Stay tuned for more Sitecore related articles.

Till that happy Sitecoring :)

Please leave your comments or share this article if it’s useful for you.

No comments:

Post a Comment