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”;
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!