I created a CodeSmith template the other day to generate a Data Provider method that returns a single value from a query. It's not a method I'll use a lot since if retrieving data I'm usually populating an object, but still a task I wanted to automate. Below is an example of the template's generated output.
I wanted the ability to return integers, strings and DateTime types, so the best way to do that was using a dropdown selection in the CodeSmith template. The dropdown is shown in the template execution dialog.
The dropdown is populated from an Enumerator created in the template's CodeBehind
public enum DataSelectType
{
Integer,
String,
DateTime
}
declared as type DataSelectType in the template .CST header.
<%@ Property Name="DataTypeReturned"
Type="DBVTHelper.DataSelectType"
Category="2. Return Value Settings"
Description="Type of Data returned: int, string, DateTime currently supported" %>
To create the first line in the method, DateTime completeTime = Convert.ToDateTime("6/9/1969"); the .CST code would be
<%= InitVariable(DataTypeReturned, VarReturned) %>
which calls a method in the .CST's <script /> area called InitVariable()
public string InitVariable(DataSelectType dataSelectType, string _varReturned)
{
switch (dataSelectType)
{
case DataSelectType.String:
{
return "String " + _varReturned + " = String.Empty;";
}
case DataSelectType.DateTime:
{
return "DateTime " + _varReturned + " = Convert.ToDateTime(\"6/9/1969\");";
}
default: return "int " + _varReturned + "= 1;";
}
}
You'll find examples of dropdown property selections in the CodeSmith sample templates.