TDS allows developers to use T4 templates to parse their Sitecore items and generate code based on the contents of the items.

The code generation feature of TDS takes a slightly different approach to using T4 templates than other code generation techniques. Normally, the T4 template parses many files and generates a single code file based on the result of the full parse. The approach for TDS is to determine when a template file has been updated from Sitecore and call a T4 template for just that template. The reason for this is that in many cases a developer makes a few small changes to a template and none of the other templates are effected. To improve the speed of code generation, TDS will cache the output of the T4 templates for each template and only re-generate the code for templates that have changed.

Another benefit of TDS's unique template generation techniques is the ability to assign different T4 templates to Sitecore templates or groups of Sitecore templates. This is done through an inheritance mechanism on a per item basis.

To use Code Generation in TDS 4.0, check the "Enable Code Generation" check box and add new T4 templates to the "Code Generation Templates" folder in the TDS project.

Enable Code Generation

Select this option to enable the Code Generation function in TDS.

Target Project

The target project is a Class Library or Web Project where TDS will place the generated code file. This must be a project in your solution.

Code Generation Target File

This field allows the developer to specify the path and filename of the generated code file. The value in this field is relative to the root of the project specified in the "Target Project" field.

Base Namespace

TDS automatically passes a calculated namespace string to the T4 template when generating code for an item. The namespace is calculated by starting with the contents of the base namespace field and appending the names of the parent folders seperated by '.' characters. This provides a convenient way for the template to render the namespace of the generated code.

Each TDS item has a property called "Namespace" in its property page (F4) that allows this value to be overridden. The overridden value will be used as the basis for all new namespaces under the item.

The developer doesn't need to use the calculated namespace while generating code, it is provided to help make namespace generation easier.

Header Transform File

The header transform file is the T4 template that is run everytime TDS needs to generate code. The result of this T4 template is placed at the start of the generated file. This is usually a good place to put #using statements.

Base Transform File

The base transform file is a T4 template, located in the "Code Generation Templates" folder of the TDS project. This T4 template can be used to generate code for all Sitecore items in the project. In most cases, the value will be set to the default value "<None> (Do not generate)", since the developer will most likely want to override the template value using the items "Code Generation Template" property lower down the Sitecore content tree.

Code Generation Fields

TDS has access to all fields for an item when building the data structure to pass into the T4 template. Passing all field values into the T4 template isn't efficient because some items may contain many very large fields. Use the Code Generation fields to select the fields you want your T4 template to receive from the Sitecore item.

Field to Add

The name of the field to add to the Code Generation fields list when the "Add" button is clicked.

Additional Code Generation Properties

Allows the developer to add additional properties that may be useful during code generation. These properties are passed to the T4 template in a Dictionary when generating code for each item.