Dave Burke : Freelance .NET Web Developer specializing in Online Communities

CodeSmith Core: Beyond The Enum

I needed application launch logic for a Windows Forms control that would launch the appropriate application (from default and user preferences) based on document type. 

Enter CodeSmith.

The default DocTypes and their associated applications were located in a SQL table.  I needed to pull numbers and a string value from the table to generate an XML file and a Switch method.  This is similar to the Enum template I use occasionally, so I expanded this template to crank out the XML and Switch method.

The properties match the table ID and ApplicationType fields, add the table, the enum name (which also serves as the Switch Method name and XML element names), and click "Generate."  The result is this code.   (Note, duplicate values make the enumerator generated unusable, but who cares when we're talking microseconds. :-)





I added two methods in my CodeSmith DBVTHelper.cs codebehind class to create the Switch and XML.  They look like so.

public string CreateEnumCases(TableSchema table, string memberNameField, string memberValueField)
{
    StringBuilder sb = new StringBuilder();
    System.Data.DataTable dt = table.GetTableData();
   
    for (int p = 0 ; p < dt.Rows.Count; p++)
    {
        sb.Append("case " + dt.Rows[p][memberValueField].ToString() + ":\r\n");
        sb.Append("\t" + GetLowerWithUnderscoreName(memberNameField) + " = \"" + dt.Rows[p][memberNameField].ToString() + "\";\r\n");
        sb.Append("\tbreak;\r\n");
    }
    return sb.ToString();
}

                   
public string CreateEnumXML(TableSchema table, string memberNameField, string memberValueField, string enumName)
{
    StringBuilder sb = new StringBuilder();
    System.Data.DataTable dt = table.GetTableData();
   
    for (int p = 0 ; p < dt.Rows.Count; p++)
    {
        sb.Append("<item id=\"" + dt.Rows[p][memberValueField].ToString() + "\">\r\n");
        sb.Append("\t\t<" + enumName + ">" + dt.Rows[p][memberNameField].ToString() + "</" + enumName + ">\r\n");
        sb.Append("</item>\r\n");
    }
    return sb.ToString();
}

which hands off the splendid strings to the template.cst code


No follow-up changes were required at all to apply this code to my application.  Definitely a timesaver and productivity booster.  That CodeSmith template gave an extra 20 minutes of dog walking time per week!




Technorati Tags: ,

Comments (2) | Post RSS RSS comment feed

Posted on 2/15/2006 12:25:00 PM by Dave Burke
Categories: .NET | CodeSmith
Tags: no tags for this post

Related posts

Comments (2) -

2/15/2006 2:16:53 PM Permalink

Dave,

Is there a reason why you do it this way other than simply launching the file and letting the user's file associations take care of which app runs?

Matt

mabster |

2/15/2006 3:02:17 PM Permalink

Good question, Mabster Man.  If launching the file alone, you don't know what you're gonna get.  For instance, just launching a PPT, DOC, XLS (most office documents) and you're firing up IE rather than opening the files in their native apps.  Unless I'm doing something wrong.  

Also, we want to give users the ability to, say, open Photoshop with image files or something else if they want rather than go with system file association defaults.

Thank you for that excellent question, Maberino.

daveburke |


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke