Sample CodeSmith and Code Snippets Comparison

Over the weekend I posted on a CodeSmith template I created to retrieve a data layer DataTable and called a utilities function to populate an ASP.NET dropdown control.  In that post I questioned whether that particular code pattern was an efficient use of CodeSmith, so with those reservations in mind I thought I would create the same code using Code Snippets in VS2005.  The bottom line is that for this example, Code Snippets is a far more efficient approach than using CodeSmith.

The generated output:

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

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

Here is what the Code Snippet that retrieves the User DataTable looks like in the IDE as well as the snippet to populate the DropDown list.

The snippets were very easy to create.  What I find most interesting (and efficient) is that snippets can be launched from their shortcut, so you never have to leave the keyboard.  Example: the oUIUtils class has to be declared, so that’s a simple “uiutils” shortcut (shown below) which generates the class reference (also shown.)  It beats the heck from dragging code fragments from the toolbox!

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.