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:
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 ->
Base Project Transform File ->

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{
        Guid Id{ get; }
        Language Language{ get; }
        int Version { get; }
    public abstract partial class GlassBase : IGlassBase{
        public virtual Guid Id{ get; private set;}
        public virtual Language Language{ get; private set; }
        public virtual int Version { get; private set; }
        public virtual string Url { get; private set; }

Note: Please have a look on below url if you faces any issue regarding t4 template.

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