CodeSmith 3.1

Reduce Repetitive Coding

Brian Noyes

October 30, 2009

4 Min Read
ITPro Today logo in a gray background | ITPro Today

asp:review

 

CodeSmith 3.1

Reduce Repetitive Coding

 

By Brian Noyes

 

Visual Studio does a pretty good job of generating codefor a lot of common coding tasks. You bring up the designer, you drag, youdrop, and voil ! hundreds or thousands of lines of code might be written foryou.

 

Unfortunately, the code that is generated is not alwaysexactly what you want. So you need to tweak. The code generated is also oftentoo fine grained. You have hundreds of tables in your database, but to generatetyped data sets to do CRUD operations for those tables you have to go throughthe Visual Studio designer once for each table. Do you want to wrap thosetables in CRUD stored procedures? Break out your favorite text editor and planon spending a week or so of painful and repetitive cut and paste coding.

 

CodeSmith 3.1 solves these problems by giving you apowerful code-generation engine that you control through easy to writetemplates. CodeSmith allows you to generate huge amounts of code, all complyingwith whatever coding patterns and standards that you want, all with a push of abutton. You can code templates for your CRUD stored procedures, data accesscomponents, business objects, and UI, or anywhere else that you see repeatingpatterns of code emerging.

 

Instead of having to generate code one class at a time, asis done with most designer features in Visual Studio, you can generate anentire layer of classes with the same pattern, such as all CRUD storedprocedures for the tables in a database, or a set of data access componentsthat work against all the CRUD stored procedures in your database. You can alsouse one of hundreds of available templates that either ship with the product orare available online from the community template sharing forum on the CodeSmithsite (http://www.codesmithtools.com/support/forum/default.aspx).

 

The way CodeSmith works is that you bring up a template inthe CodeSmith Explorer that is designed to generate the code pattern youdesire. Simply fill in a few parameters that the template prompts you forthrough a property grid, press a button, and presto! the CodeSmith enginegoes to work and spits out in milliseconds code based on the template and theparameters you provided. If you need to customize what is being generated, you simplyedit the template itself.

 

The templates are specified using a syntax that is verysimilar to ASP.NET markup syntax. Any content in the template that is notinside a script block is simply output as is. You then embed script blocks inC# or Visual Basic to generate the dynamic aspects of the code, such as thoseparts 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 andthe code within them can be called from your scripts in the template.

 

To edit these templates you can use the CodeSmith Studiothat comes with the Professional version (see Figure 1). This IDE looks andfeels a lot like Visual Studio itself, but presents a tailored and focusedenvironment for template authoring, editing, and debugging. You get thebenefits of automatic statement completion, injection of predefined codeblocks, compile-time error checking, and a chance to run your templates in theenvironment. You can also use the Visual Studio integration custom tool to doyour 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 theStandard edition, which includes only the engine and the CodeSmith Explorer toexecute your scripts. You can then use any text editor to create and edit yourtemplates.

 

I ve been using CodeSmith for a while, and can t imaginedoing large production coding projects without it. The net result of usingCodeSmith is that you ll end up with cleaner, more correct, and moremaintainable code in a fraction of the time.

 

Brian Noyes is asoftware architect, trainer, speaker, and writer with IDesign, Inc. (http://www.idesign.net). He speaks at majorindustry 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 hitthe shelves in January 2006, followed shortly thereafter by his next book, Smart Client Deployment with ClickOnce.Contact Brian at mailto:[email protected].

 

Rating:

Web Site: http://www.codesmithtools.com

Price: Standard,US$49; Professional, US$299

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like