Using a Generator Project inside Visual Studio

You can manage any CodeSmith Generator project from Visual Studio, so code generation stays part of your normal workflow and you don't have to switch applications.

Creating a Generator project

  1. In Solution Explorer, right-click a project, or a folder in a project, and add a new item.

  2. In the dialog, select the CodeSmith Generator category to show only the Generator item templates.

    New item dialog showing the CodeSmith Generator item templates, with Generator Project selected

  3. Select Generator Project, then click Open, or double-click Generator Project.

Managing a Generator project

Right-click a project file (.csp) in Solution Explorer to manage it.

Solution Explorer context menu for BusinessObject.csp with Manage Outputs, Generate Outputs, Add Output and Output Options

Command What it does
Manage Outputs Opens the Manage Outputs dialog.
Generate Outputs Generates all of the outputs in the project.
Add Output Adds a new output. See Adding and editing outputs.
Output Options Controls what happens to the outputs after generation. Available only in Visual Studio.

Output options

Output Options submenu with Add Outputs To Project, Hide Outputs and Generate On Build checked

Option What it does
Add Outputs To Project Adds the project's outputs to your Visual Studio project.
Hide Outputs Nests the outputs under the Generator project file in Solution Explorer, as shown below. You can collapse the nested files, which also discourages other developers from editing generated code.
Generate On Build Regenerates the outputs every time you build. This is the same as setting the project file's build action to Generate; see Generating on build.

Generated files nested under BusinessObject.csp in Solution Explorer

Note

Turning off Generate On Build also removes the Generator dependencies from your Visual Studio project.

Generating on build

To regenerate a Generator project's outputs every time you build, set the project file's Build Action to Generate in the Properties window. The whole generation process then runs before your project builds, and the outputs can be included in your Visual Studio project.

Properties window for BusinessObject.csp with Build Action set to Generate

For example, a class library contains a BusinessObject.csp project with four outputs, all generated from the same BusinessObject.cst template:

  • Order.cs
  • Product.cs
  • Profile.cs
  • Supplier.cs

With the build action set to Generate, any change you make to the database schema, XML metadata or template is picked up the next time you build. This saves you from updating every class by hand during development.

Generation feedback

The Output window shows the generation messages before the build starts, as shown below.

Visual Studio with generated business object classes in Solution Explorer and generation messages in the Output window

Nesting generated files under a parent file

A template can nest a generated file under another file in Solution Explorer, as in Visual Studio's DependentUpon hierarchy. In the example below, Order.generated.cs is nested under Order.cs.

Solution Explorer showing Order.generated.cs nested under Order.cs

To do this, call the RenderToFile overload that takes a parent file. Generator adds metadata to the output file that Visual Studio uses to create the hierarchy. Then add the Generator project to your Visual Studio project and generate its outputs; Generator adds the outputs to your project with the hierarchy.

// Create the sub-templates. Create<T>() wires everything up automatically.
EntityGeneratedClass entityClass = this.Create<EntityGeneratedClass>();
EntityEditableClass partialClass = this.Create<EntityEditableClass>();

string className = type.Name;

string parentFileName = className + ".cs";
parentFileName = Path.Combine(OutputDirectory, parentFileName);
// Output the parent file
partialClass.RenderToFile(parentFileName, false);

string fileName = className + ".Generated.cs";
fileName = Path.Combine(OutputDirectory, fileName);
// Output the child (dependent) file, linked to the parent
entityClass.RenderToFile(fileName, parentFileName, true);

Setting the build action of generated files

When a template generates files into a Visual Studio project, you may want to set a generated file's build action. For example, an EDMX file should always have the build action EntityDeploy.

Build Action list for PetShop.edmx with EntityDeploy selected

To do this in your template:

  1. Create an OutputFile for the file you're generating.
  2. Add the build action to its Metadata dictionary.
OutputFile output = new OutputFile("PetShop.edmx");
output.Metadata.Add("BuildAction", "EntityDeploy");
this.RegisterOutput(output);

Then make sure the build action is applied, in one of two ways:

  • If your template is a sub-template that generates the file's content itself, call the template's RegisterOutput method, as shown above.
  • If you call RenderToFile on a template, use the RenderToFile overload that takes an OutputFile. See the CodeTemplate members in the API documentation.