CodeSmith for Code Changes

One of the greatest benefits of using a code generation tool like CodeSmith comes not from generating methods and objects the first time, but when you need to change that code.  Using CodeSmith to regenerate template-based blocks of code is potentially so much more efficient than editing code by hand.  Below is an example of responding to field name changes of a SQL table with CodeSmith and the time savings that results.

In the example I am migrating data from Access tables to SQL Server using a Community Server background task.  I needed to add a “VendorNo” field to the Company Table and since I was adding a field I decided to change several field names of the company tables as well.  CompanyAddress became Address, CompanyPhone became Phone, and so on.  Normally that would require some ugly code editing, but making the changes shown below and elsewhere in the app required literally only minutes with CodeSmith.

Above is the method that imports company data into SQL Server.  The Business Object CompanyImport that was generated from the updated table no longer contains CompanyAddress, CompanyPhone, etc.  Time to execute my “Add SQL Update-Insert Parameters.cst” CodeSmith Template.

The resulting cmd.Parameters.Add() statements were generated from the above template in seconds, which compiled clean and contained all of the field name changes as well as the new “VendorNo” field.

Article written by

A long time developer, I was an early adopter of Linux in the mid-90's for a few years until I entered corporate environments and worked with Microsoft technologies like ASP, then .NET. In 2008 I released Sueetie, an Online Community Platform built in .NET. In late 2012 I returned to my Linux roots and locked in on Java development. Much of my work is available on GitHub.