The Composite CodeSmith Template

The term Composite CodeSmith Template refers to generating code across several tiers of an application, writing code for multiple classes in a single template, somewhat of a cross between a single template and a CodeSmith Project.  I don’t know if there’s any conventional term for this, but I needed a folder name to stick this type of template and came up with “Composite.”  This is by no means anything new, but I’m blogging about it because there’s a lot of potential here.  I also know that I’m only part of the way along the code generation journey to where I want to be with this.  CodeSmith is very addictive that way, because it seems you can always find new efficiencies the longer you code with CodeSmith in mind.

I needed to perform a SQL update in Community Server from a Chameleon form.  This consists of writing a method call in the form’s OnSubmit()…to a method in the project’s root namespace that sits between the UI and the data layer (a .Text convention that carried over to CS)…with an abstract method stub in the SQL Data Provider’s interface method…and finally the SQL Data Provider performing the SQL update.  Not a lot of code, but unnecessarily time consuming.  Enter the Composite CodeSmith Template.

Here’s the generated code, with the template execution window below it that lists the template’s properties.   The idea is to write the initial method with the template taking care of the rest.  What remains after running the template is entering the sql string (or stored procedure name and its params, as there’s a CommandType option in the template), then copying and pasting appropriately.   I’m thinking this can be improved upon in methodically adding CodeSmith “tags” in the source code so that the template writes directly to the class files in those tagged locations to eliminate the copy-pasting.  That’s a different matter, but like I said, there are always more efficiencies to be realized with CodeSmith over time.

 

 

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.