Larkware

We get up early so that you don't have to.

Review: CodeSmith 3.0

CodeSmith 3.0, starting at $49
CodeSmith Tools, LLC
Dallas, Texas
http://www.codesmithtools.com

CodeSmith is a template-based code generator that can produce code for any text-based language: C#, Visual Basic .NET, FORTRAN, T-SQL, whatever; as long as it's text, CodeSmith can output it. CodeSmith's template syntax is based on ASP.NET, so it's easy to pick up for most .NET developers. It comes with two different GUIs: CodeSmith Explorer, which provides a simple way to launch templates and generate code, and CodeSmith Studio, which is a full-blown IDE for template development, featuring IntelliSense-like statement completion, color-coding, incremental search, outlining, tabbed windows, and all the other goodies you'd expect from an IDE. There's also a console version that you can easily integrate into your own build process, flexible strategies for merging generated code with custom code, an API for integration with relational data sources, and the ability to hook up your own custom metadata sources.

Here's a tiny bit of one of the sample templates, just to give you a feel for the flavor of CodeSmith. This particular one uses VB as the scripting language (you can also use C# or JScript as the language that actually drives CodeSmith) and creates a class to manage a sorted list:


<%@ CodeTemplate Language="VB" TargetLanguage="VB" Description="Generates a strongly-typed collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index." %>
<%@ Property Name="ClassNamespace" Type="System.String" Optional="True" Category="Context" Description="The namespace that the generated class will be a member of." %>
<%@ Property Name="KeyType" Type="System.String" Category="Context" Description="The type to use as a key in the collection." %>
<%@ Property Name="ItemType" Type="System.String" Category="Context" Description="The type to use as an item in the collection." %>
<%@ Property Name="ClassName" Type="System.String" Category="Context" Description="The name of the class to be generated." %>
<%@ Property Name="Accessibility" Type="AccessibilityEnum" Category="Options" Description="The accessibility of the class to be generated." %>
Option Strict On

Imports System
Imports System.Collections

<% If Not ClassNamespace Is Nothing AndAlso ClassNamespace.Length > 0 Then %>
    Namespace <%= ClassNamespace %>
<% End If %>

<Serializable()> <%= GetAccessModifier(Accessibility) %> Class <%= ClassName %>
Implements IDictionary, ICloneable


#Region "Member Variables"

Private Const DEFAULT_CAPACITY As Integer = 16

Private m_keys() As <%= KeyType %> 
Private m_values() As <%= ItemType %> 
Private m_count As Integer
Private m_version As Integer
Private m_comparer As IComparer
Private m_keyList As KeyList
Private m_valueList As ValueList

You get the idea. The Property directives at the top of the file define the properties that CodeSmith will prompt the user for; when the code is generated, these properties are substituted at appropriate places in the generated code. Scripting is used to control the generation process; you've got access to the entire .NET Framework to help you in that. So, you can do pretty much anything you like at generation time.

Advanced capabilities you'll find here include the ability to separate scripting logic from template code by putting it in a code-behind file, calling into sub-templates, compiling templates into assemblies for programmatic execution, support for dealing directly with XML files, active code generation via merge strategies, and the ability to define a property of any .NET type and with a custom editor for data input. If you're already a CodeSmith user, you'll find a lot of new features in 3.0, including the new XmlProperty directive, overhauls to merge strategies and the console client, the ability to execute SQL scripts after generating them, and the ability to render to more than one TextWriter at the same time.

CodeSmith 3.0 is the first release without a free version (the standard version of CodeSmith 2.6 will remain available for free). You can download a 30-day trial for free, but after that you need to pay to keep using CodeSmith. The $49 CodeSmith Standard gives you most of the CodeSmith features, the console client, and the basic CodeSmith Explorer GUI client. The $299 CodeSmith Professional adds the CodeSmith Studio IDE, Visual Studio .NET integration, and a few other advanced features. Either way, you get a batch of sample templates, and you'll find a lot more user-contributed templates on the CodeSmith support Web site.

You'll find two broad types of code generators out there. Some concentrate on taking a database or an XML schema and building up an application from it, aiming to do most of the work of bringing your data to a Web or a Windows interface. CodeSmith is the other type: a general-purpose tool that can generate pretty much anything you can think of. You'll need to spend some time learning how to use it (and plan on spending some of that time online, because the documentation with the application is very sketchy). But once you understand how all the pieces fit together, you can use it anywhere that you spot patterns in your code, whether that code is SQL to generate repetitive stored procedures or classes that you use to hook up routine sorts of objects over and over again. I find it a great tool to have handy for all sorts of utility coding purposes.

Full disclosure: I currently have a contractual relationship with CodeSmith Tools, LLC, at the moment. They're paying me to write the help file that will ship with a future version of CodeSmith 3.x. I was a happy CodeSmith user before this opportunity came along, but you should still keep this in mind when reading the above review.

  Click for larger screenshot

Mike Gunderloy is the lead developer for Larkware and author of numerous books and articles on programming topics.