T4

The Microsoft.Blueprints.Contrib project contains a simple, yet powerful pattern for working with T4 Templates. This pattern reduces duplication and increases readability of code.

T4TemplateBuilder

The T4TemplateBuilder gives you a fluent interface for manipulating and transforming T4 Templates. There are two main ways of using the T4TemplateBuilder, setting properties manually, or dynamically.

Manual

Using the T4TemplateBuilder in Manual Mode is very simple.

T4Template template = T4TemplateBuilder
                        .Initialise(context)
                            .SetTemplateFileName("MyT4Template.tt")
                            .SetTargetFileName(this.envManager.SelectedProjectDirectory, "Folder\MyGeneratedFile.cs")
                            .SetProperty("MyProperty", "MyValue")
                            .Transform()
                            .SaveToFile()
                        .Finish();
The only line above that might need explaination is the this.envManager.SelectedProjectDirectory code fragment . This is an instance of the EnvironmentManger, see the Environment wiki entry for more information.

Dynamic

Using the T4TemplateBuilder in Dynamic Mode enabled some interesting transformation scenarios. In Dynamic Mode, the T4Template is analysed and all relevant properties are extracted automatically and a pushed into a Dictionary<string, T4PropertyData>. Once the properties have been dynamically loaded into the Dictionary<string, T4PropertyData> you can use the DynamicUpdate delegate to create a lambda function to generate the missing values for the Dictionary<string, T4PropertyData>. In the example below we use a lambda to create new guid for the T4Template.

DynamicUpdate strategy = template =>
                         {
                             template.Properties[template.Properties.Keys.FirstOrDefault()] = 
                                 new T4PropertyData(Guid.NewGuid().ToString(), typeof(string));
  
                             return template;
                         };
    
T4Template template = T4TemplateBuilder
                        .Initialise(context)
                            .SetTemplateFileName("MyT4Template.tt")
                            .SetTargetFileName(this.envManager.SelectedProjectDirectory, "Folder\MyGeneratedFile.cs")
                            .LoadTemplate()
                            .DynamicallyGenerateProperties()
                            .DynamicallySetPropeties(strategy)
                            .Transform()
                            .SaveToFile()
                        .Finish();

DynamicUpdate

The DynamicUpdate delegate allows you use combine the power of the Builder Pattern (providing a fluent interface) with extensibility of injecting your own custom code to update the template properties, thus not limiting your options. The example below shows a more complicated scenario that makes use of the T4TemplatePropertyEditor. In this example a lamba that invokes the T4TemplatePropertyEditor and injects it into the T4TemplateBuilder via the DynamicUpdate delegae which is passed into the DynamicallySetProperties method.

DynamicUpdate strategy = template =>
                         {
                             using (T4TemplatePropertyEditor editor = new T4TemplatePropertyEditor(template))
                             {
                                 if (editor.ShowDialog() == DialogResult.OK)
                                 {
                                     template.Properties = editor.Properties;
                                 }
                                 else
                                 {
                                     throw new OperationCanceledException("User selected Cancel. Properties not updated.");
                                 }
                             }
  
                             return template;
                         };
  
T4Template mytemplate =  T4TemplateBuilder
                            .Initialise(context)
                                .SetTemplateFileName(TemplateFiles.BuildProperties)
                                .SetTargetFileName(this.envManager.SelectedProjectDirectory, TargetFiles.BuildProperties)
                                .LoadTemplate()
                                .DynamicallyGenerateProperties()
                                .DynamicallySetPropeties(strategy)
                                .Transform()
                                .SaveToFile()
                            .Finish();
For information on how to manipulate the files generated by the T4TemplateBuilder, see the documentation on the helper classes in the Environment namespace.

Last edited Feb 27, 2009 at 5:37 PM by HowardvanRooijen, version 7

Comments

No comments yet.