CodeSmith on Patterns for 1/28/06

There are patterns published in books and patterns we use in our applications.  We know when we’re typing out a code pattern that can probably be auto-generated.  But it can seem like real work to sit down and write the CodeSmith code for the pattern instead of mindlessly cranking out syntax.  It takes time, too.  So some of the questions might be

  • “How much time does it take to write the template?”
  • “How often will I use the template in the future?”
  • “Is the pattern fixed or is there too much variation to justify a template?”
  • “How many CodeSmith properties are required?  How long will it take to execute the template each time its used?”
  • “What’s the cleanup factor, if any?  Any additional typing required after the template is executed?”
  • “How long has it been since I CodeSmith’d something?”  (If it’s been a while, just do it anyway. 🙂

Here’s a example of that thought process in action on a simple pattern used to grab a DataTable from the data layer and use it to populate a dropdown list.  The generated output is:

public DataTable GetUsers(int _userID)
{
    string sql = “select UserID, UserName from ps_Users where UserID = ” + _userID + ” and active = 1 order by UserName”;
    return SQLHelper.ExecuteDataTable(sql);
}

oUIUtils.DoDropDownList(ddUsers, oSQLUsers.GetUsers(userID), “UserID”, “UserName”, _userID.ToString());

Not much code, but that’s a bit deceiving since there are several properties.  Here is the essential CodeSmith template code for the above:

To generate these few lines of code requires entering 6 properties shown below, which gets to the question of how much time is required to execute the template.  It’s also possible to require some post-execution typing depending on the query, though that is very little.  Bottom line, this might not have been a great use for CodeSmith, but simply as a learning exercise “Thinking CodeSmith” while writing code, that’s gotta be good.


Technorati Tags:

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.