Anatomy of a CodeSmith Project File

CodeSmith Project File

At a high level, a CodeSmith Generator Project file manages all of the template properties in a given CodeSmith Project. These files use XML to declaratively manage all of the CodeSmith template properties for each of your template Outputs using a .csp file extension.

Every CodeSmith Template uses a Property Sheet that help drive template meta data in your generation process.  When the property sheet is saved, it is saved as a Property Set, an XML serialized version of your properties and their values.  Saving an XML version enables you easily recover all of the options you designated while configuring your Property Sheet when this file was created.

Header

The XML Header specifies the current XML Schema Definition for a CodeSmith Project. The CodeSmith node also encapsulates the entire body of the CodeSmithProject XML Files.

<?xml version="1.0"?>

<codeSmith xmlns="http://www.codesmithtools.com/schema/csp.xsd">

This csp.xsd can be found at INSTALL DIR/Schemas/csp.xsd

Defaults

Default Template - You can configure a CodeSmith Project to use Default Template, when configured, each of the Property Sets that do not have a Template assigned to them will automatically use the Default Template to execute and run.

<defaultTemplate path="businessobject.cst" />

Default Properties - You can configure default properties to use within your CodeSmith Project. These properties are available to all of the templates assigned within your CodeSmith Project. These are especially useful to define properties that are fairly static in nature.

<defaultProperties>

    <property name="ClassNamespace">CompanyName.RootNamespace</property>

  </defaultProperties>
Variables

The CodeSmith Project supports variables that can be used in the property sets. Variables are an easy way to have a common piece of data that is stored in only one place.

You can edit the CodeSmith Project manually to place variables. The variable for format is its name surrounded by $(), ie, $(ConnectionString1).

<variables>
    <add key="ConnectionString1" value="Data Source=(local);Initial Catalog=PetShop;Integrated Security=True" />
    <add key="ProviderType" value="SchemaExplorer.SqlSchemaProvider,SchemaExplorer.SqlSchemaProvider" />
</variables>

<propertySet name="Dbml" template="CSharp\Dbml.cst">
    <property name="SourceDatabase">
      <connectionString>$(ConnectionString1)</connectionString>
      <providerType>$(ProviderType)</providerType>
    </property>
</propertySet>
Property Sets

For every template output you will see the associated PropertySet and it's output. The PropertySet may or may not have a template defined, if it does not, it will use the default template value for generation. The Properties defined within each of the property sets are specific to this Output and are not shared with any other Outputs.

<propertySets>

    <propertySet output="Product.cs">

      <property name="SourceTable">

        <connectionString>Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Petshop.mdf;</connectionString>

        <providerType>SchemaExplorer.SqlSchemaProvider,SchemaExplorer.SqlSchemaProvider</providerType>

        <table>

          <owner>dbo</owner>

          <name>Product</name>

        </table>

      </property>

    </propertySet>

    <propertySet output="Order.cs">

      <property name="SourceTable">

        <connectionString>Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Petshop.mdf;</connectionString>

        <providerType>SchemaExplorer.SqlSchemaProvider,SchemaExplorer.SqlSchemaProvider</providerType>

        <table>

          <owner>dbo</owner>

          <name>Orders</name>

        </table>

      </property>

    </propertySet>

  </propertySets>
Defined Property Sets

Each property set is defined between propertySet nodes as depicted in the image below.