Product Reviews

Generating Code with CodeSmith

CodeSmith Studio supports the creation of code-generation projects for a wide variety of outputs-though it has limited integration with Visual Studio.

Visual Studio employs code generation to create the class files behind DataSets and Entity Framework models. Now CodeSmith Studio by CodeSmith Tools LLC lets you create your own code-generation solutions using a template-based approach.

In a CodeSmith template you place static text for code that doesn't change from one instance to another, along with executable code-in the .NET language of your choice- that generates code based on inputs you provide. Creating a CodeSmith template is easy, and the tool includes default templates for Visual Basic, C# and JScript.

Once a template is created, you can add your "code that writes code" and use CodeSmith's built-in objects to implement standard code-generation activities.

CodeSmith also lets you define properties within your code template. To generate code, just set values in those properties and execute your template; in your template you use those property values to control what code you generate. You can also create, save and copy sets of property values.

CodeSmith's SchemaExplorer object lets you process objects in a database-including stored procedures-to use them as inputs to your generation process. You can also create a Schema provider for your own input file format and use SchemaExplorer to read your input file.

[Click on image for larger view.]
Figure 1. To get design-time support when editing code-generation templates, you'll have to open the CodeSmith Studio.

Integrating with Visual Studio
CodeSmith provides some integration with Visual Studio. The CodeSmith Explorer window in Visual Studio lets you review your available templates and generate code from them. You can also add CodeSmith projects to a Visual Studio project to manage more complex code-generation processes. CodeSmith's Active Snippets allow you to insert names of templates into your code and have the code generated from the template inserted into your code.

However, there's no Visual Studio editor for template files. You must use CodeSmith Studio if you want editing support while writing "code generation code." Nor can you tie your code generation into Visual Studio events as happens with DataSets and Entity Framework models-code will only be automatically generated when you build your application. New code files are added by CodeSmith-which is an external program to Visual Studio-so whenever you generate code or edit files in CodeSmith you'll get a "project has been updated" dialog in Visual Studio. CodeSmith doesn't include support for editing input files, but you can create property editors for your template's Property window to support the input of complex values into the Properties window.

CodeSmith does more than create .NET/ Visual Studio solutions and generate single, standalone class files. It generates multiple files, merges code into existing files and creates non-.NET code, including stored procedures. If you generate stored procedures, for instance, CodeSmith's ExecuteScript method executes your SQL so your new stored procedure is added to your database.

With CodeSmith Studio-for a very reasonable price-you can add code generation to your development toolkit. Code generation with CodeSmith isn't as seamless as the tools that come with Visual Studio, but it does provide a flexible means of generating code of any kind.

CodeSmith Studio

CodeSmith Tools LLC
Phone: 800-719-3309
Price: Standard Edition, Personal License $79; Professional Edition, Personal License $199
Quick Facts: Implements template-based code generation at the right price
Pros: Infrastructure to create code-generation solutions for a wide variety of scenarios
Cons: Only partial integration with Visual Studio

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at

comments powered by Disqus