Thursday, February 14, 2013

C# Code Generation with TDS 4.0 for Sitecore

This post is about generating code using TDS 4.0 and Sitecore based on T4 Scripts. Generating code is like a 'must have' on every project. It simplifies,.. well,.. a lot!
I have been spending quite some time on getting the code generation to work using Hedgehogs TDS4.0 and Sitecore. Only little information can be found online, so i thought to share my knowledge with you in order to get you up and running in little time.
At the bottom of this post you'll find some links i used in my search and where possible i'll try to use screenshots to illustrate my words, please click on them for larger versions.



Sitecore Solution and Visual Studio Project setup

Developing Sitecore Solutions can be done in roughly two kind of setups.
  1. Development is done side by side of the Sitecore instance and deployed directly into the 'bin' and 'layouts' folders (for example) when you build your solution.
  2. Development is done in a separate solution, in projects not directly linked to your Sitecore instance but during buildtime files are transfered and merged to the sitecore solutions folder.

This post will be based on the second scenario. Read this blog post by John West if you are looking for insights on scenario one. Some further reading on scenario two can be found here.

Lets get started, lets get generating

First of all, you will need a local instance of Sitecore and a visual studio solution.
Make sure Sitecore Rocks is up and running so you can do some easy testing on creating templates and generating the code. If you want Sitecore Rocks to add items directly to your Visual Studio Solution, make sure the sitename in Rocks is the same as the URL used in the TDS Projects.
At the right you see sitecore rocks; The sitecore explorer. Click for larger image (2).


Add a TDS project to the soltution and set it up

To start you obviously need a TDS project in the solution. Make sure it connects to the master database for now. Set the deploy and Website-folders according to your local instance. Assign the Source web project in the general tab, this is the project that will be holding the generated code later on.









Image 3, project settings

Now navigate to the page that is about build and set the different fields so they match your setup/instance.
The properties of the TDS Master project. Click for larger image.


image 4


image 5

Leave all default code generation settings as they are set upon enabling the feature. Well look into that later.
After saving the TDS project, go to the solution explorer in Visual Studio and see that a node is added to the  project (image 6):

The folder that was added will hold all the T4 templates to use in your solution. By default TDS does not ship with any template that works out of the box. This is not a problem, but did not meet my expectations.







Download some working and simple code generation templates

You will need set of working T4 Templates to get you started. 

In the zip you'll find 3 files:
Header.tt is a file that you should use as a (run once) header for the generated file. Set this header in the TDS project settings, see image 5.
RenderClassesFromTemplates.tt is the template that is called for each Sitecore item. If you specify this file in the TDS Project Properties (image 5) then it executes for every item in your TDS Project. i only want this to run on templates, so i set it via the properties in the solution explorer: (image 7)
Do not specify any code generating options on the child nodes. The child nodes inherit the parentsettings by default. For example: if you do not specify any code generation template on the folder as shown, the template form the properties file is used
Inlcudes.tt is an empty sample file to show you how include files are used in T4 templates.

When you have added the three files to your solution, you should be ready to generate some code. Right click on the TDS project and choose 'Re-generate code for all items'

A new file should now be added to the project you specified in image 5, with the name you specified in image 5. Your code could look like, and should look like :)














Now it's up to you

You should have some generated code by now, if not, or have question, drop me a line below.
Try some more searching the web, when it comes to the specifc T4 script language. Before starting, make sure you know what you want to generate. create an example and use that to work towards.

Since you have coupled Sitecore Rocks to this TDS project (hostheader is the same in Rocks as in TDS Properties) your changes to Sitecore will be automatically be processed. When you use Rocks to add a template with some fields and you save it, the code generation kicks in and rewrites your code.


Some best practices, please share yours below:
Start small. Download the example files, delete the content and the include files. Start your own template from scratch to better understand ow it works.
Create a example class first. Think about how you want you generated code to look like.
Familiarize your self with T4 concepts.
Typicaly you generate code for templates, but you could also create code for documentation or other files. (XML, TXT, HTML..)


References:
Team Development System by Hedgehog development and theire help page
Blog about TDS Code Generation by SitecoreCreative
Youtube movie by Sean Kearney on Code Generation (movie seems corrupted, shows black screen a lot)
T4 script examples on Github
T4 example scripts as used by the TDS manufacturer

No comments:

Post a Comment

Thank you for responding!