Design patterns provide a standard approach to attacking common programming problems; however, developers often find themselves typing the same code over and over again. Code generation addresses this issue by providing the skeleton code necessary to get started. It can speed up development and provide stability and reliability in your code. Let's take a closer look at code generation with CodeSmith.
What is it?
CodeSmith is a template-based code generator. Templates are patterns for generated code. By using CodeSmith, developers or architects may generate code for any text language. The result (generated code) is customizable via properties and includes numerous standard property types. In addition, users may create custom property types.
When targeting the .NET Framework, a property may be any .NET object that has a designer. For example, it can be as simple as a string property assigning a title. On the other hand, a TableSchema object may be used to provide access to everything about a database table.
One of the great aspects of the CodeSmith tool is its syntax, which is almost identical to the syntax used in ASP.NET. In fact, you may use C#, VB.NET, or JScript in CodeSmith templates. CodeSmith can actually output any ASCII-based language.
You can download a 30-day free trial of the standard and professional versions of CodeSmith. The professional version extends the standard version by adding an IDE, VS.NET integration, an API, and more. Check the CodeSmith Tools site for complete pricing details.
The CodeSmith tool includes numerous tools for putting code generation to good use in your next project. It includes the following features:
- Studio: Allows you to develop your own templates.
- Debugging: Debugging is simplified with robust error messages that include the line number and template related to the problem.
- XML: XML is a big part of template creation. You can easily use your own XML to drive templates. You may provide your own schema or work without one.
- Console client: The client provides a single point for automating code generation or executes one or more templates.
- SQL Script execution: You can automatically generate SQL Script when applicable for easy deployment of SQL code.
- Caching: Templates are compiled and cached to improve performance.
There are two ways to begin a CodeSmith session: CodeSmith Explorer or CodeSmith Studio. The Explorer client, which provides an interface for organizing templates, is analogous to Windows Explorer. It opens with the initial view showing all of the folders containing templates in your CodeSmith installation. A default installation includes numerous sample templates (template files use the .cst file extension).
An alternate way to use CodeSmith is via the CodeSmith Studio IDE, which is a complete template IDE. You can use it to edit, compile, and run templates. A big advantage with the Studio client is the debugging features, which streamline the process of finding any errors that may arise in your templates.
The CodeSmith Explorer client includes a subset of the features found in the Studio IDE. The Explorer is good if you just want to run templates or generate code; the Studio client is better for more heavy lifting.
Developing a template
While CodeSmith includes various sample templates, you will need to develop custom templates to take advantage of code generation in your projects. Templates are plain text files that contain three types of content:
- Code generation directives.
- Static content passed through to the generated output.
- Dynamic content that is executed by CodeSmith. This is where programming code is used.
Every template begins with the CodeTemplate directive (notice the ASP.NET similarities):
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="TR Sample" %>
Now you need to know what you want to generate. The simplest approach to determining what you want is developing example code that generates what is desired. Once you know what you want, you should break the output (i.e., what you want to be generated) into the following three areas:
- Static content (it never changes)
- Generated content
- User-generated content (the user must enter information)
Let's examine the template:
- It utilizes ASP.NET style comments. An example is the insertion of the author name and other information at the top of the file. CodeSmith inserts this section automatically, but you can easily delete it.
- The CodeTemplate directive is the first real line in the template (omitting comments). It assigns a description, the language used, and the output format (text). There are more attributes available.
- The Property directive allows properties to be added to the template. We use two properties: Boolean and string. The name attribute assigns the name that may be used throughout the template. The data type is assigned via the type attribute. The default attribute allows you to assign a default value, and the optional parameter signals whether it is mandatory (true) or not (false).
- Static content is placed directly in the template. The CodeSmith engine ignores this text and passes it straight through to the output.
- Variables may be placed in the output by using the ASP.NET syntax (<%= variable name %>) and the property name.
- Code may be used as the if statement is used to check the Boolean property. The content within the if statement is included in the output only if the property is true.
- Code blocks may be included within script tags. This makes the code accessible throughout the template. In our example, the NameMethod method is declared.
This is a very simple example that demonstrates basic features. You may be wondering how it can reduce development time when you'll need to do additional development in CodeSmith. The template system lends itself to generating any type of content, so it is easy to generate the code files you'll need. Likewise, the customization and code support make it easy to generate on the fly. The power will be more evident as you dig into the product.
Free your mind
Code generation promises to reduce development time by generating repetitious code based upon templates you develop. The CodeSmith tool provides a powerful and extensible template system that you can use to generate almost any type of output.
Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.
Miss a column?
Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.
Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a production environment on a daily basis.