Reduce Repetitive Coding
By Brian Noyes
Visual Studio does a pretty good job of generating code for a lot of common coding tasks. You bring up the designer, you drag, you drop, and voil ! hundreds or thousands of lines of code might be written for you.
Unfortunately, the code that is generated is not always exactly what you want. So you need to tweak. The code generated is also often too fine grained. You have hundreds of tables in your database, but to generate typed data sets to do CRUD operations for those tables you have to go through the Visual Studio designer once for each table. Do you want to wrap those tables in CRUD stored procedures? Break out your favorite text editor and plan on spending a week or so of painful and repetitive cut and paste coding.
CodeSmith 3.1 solves these problems by giving you a powerful code-generation engine that you control through easy to write templates. CodeSmith allows you to generate huge amounts of code, all complying with whatever coding patterns and standards that you want, all with a push of a button. You can code templates for your CRUD stored procedures, data access components, business objects, and UI, or anywhere else that you see repeating patterns of code emerging.
Instead of having to generate code one class at a time, as is done with most designer features in Visual Studio, you can generate an entire layer of classes with the same pattern, such as all CRUD stored procedures for the tables in a database, or a set of data access components that work against all the CRUD stored procedures in your database. You can also use one of hundreds of available templates that either ship with the product or are available online from the community template sharing forum on the CodeSmith site (http://www.codesmithtools.com/support/forum/default.aspx).
The way CodeSmith works is that you bring up a template in the CodeSmith Explorer that is designed to generate the code pattern you desire. Simply fill in a few parameters that the template prompts you for through a property grid, press a button, and presto! the CodeSmith engine goes to work and spits out in milliseconds code based on the template and the parameters you provided. If you need to customize what is being generated, you simply edit the template itself.
The templates are specified using a syntax that is very similar to ASP.NET markup syntax. Any content in the template that is not inside a script block is simply output as is. You then embed script blocks in C# or Visual Basic to generate the dynamic aspects of the code, such as those parts that depend on a table or class name provided at code-generation time. You can also associate code-behind files that will be dynamically compiled and the code within them can be called from your scripts in the template.
To edit these templates you can use the CodeSmith Studio that comes with the Professional version (see Figure 1). This IDE looks and feels a lot like Visual Studio itself, but presents a tailored and focused environment for template authoring, editing, and debugging. You get the benefits of automatic statement completion, injection of predefined code blocks, compile-time error checking, and a chance to run your templates in the environment. You can also use the Visual Studio integration custom tool to do your template execution right within your Visual Studio projects.
Figure 1: CodeSmith Studio template editing.
If you are working on a tight budget, you can get the Standard edition, which includes only the engine and the CodeSmith Explorer to execute your scripts. You can then use any text editor to create and edit your templates.
I ve been using CodeSmith for a while, and can t imagine doing large production coding projects without it. The net result of using CodeSmith is that you ll end up with cleaner, more correct, and more maintainable code in a fraction of the time.
Brian Noyes is a software architect, trainer, speaker, and writer with IDesign, Inc. (http://www.idesign.net). He speaks at major industry conferences worldwide, including Microsoft TechEd US, Europe, and Malaysia, Visual Studio Connections, and others. Brian s latest book, Data Binding in Windows Forms 2.0, will hit the shelves in January 2006, followed shortly thereafter by his next book, Smart Client Deployment with ClickOnce. Contact Brian at mailto:[email protected].
Web Site: http://www.codesmithtools.com
Price: Standard, US$49; Professional, US$299