oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Build a Web-Based Bug Tracking App, Part 2
Pages: 1, 2, 3, 4

Hard Coding Versus Data-Driven

Einstein One of the premises of the Tiny Bug Tracker is that the code and the user interface are both kept as simple as possible. But the words of Einstein are worth keeping in mind: "Everything should be made as simple as possible, but not one bit simpler." [1]

We may have made it a bit simpler than it should be; however, by hard-coding the prohibited status as Closed, the privileged role as QA, and the highlighted status as Show Stopper. It would not be hard to create either a data table that held this information, or, perhaps even better, in the appSettings section of Web.config.

Let's create three settings:

  1. The role that has permission to close a bug.
  2. The string that indicates the status for closing a bug.
  3. The status string that should be shown in red.

To do so, open Web.config and add an appSettings tag if you don't have one (I had a self-closing one that I opened) and this code:

<configuration xmlns="">
    <add key="RoleThatMayCloseBug" value="QA" />
    <add key="StatusForClosedBug" value="Closed" />
    <add key="StatusToShowInRed" value="Show Stopper" />

This creates the keys and the values to be retrieved in the code. Modify the sections of code that use these values as follows: first, modify the if statement within the DataBound event handler in TBTReportBug.aspx.cs as follows:

 if (li.Text.ToUpper() == 

That will fetch the word "Closed" from the Web.config file, and allow you to change the web config file without having to track down where this is used in the code. Similarly, we'll change the test for whether the user is in the role itself.

string theRole = ConfigurationManager.
if ( User.IsInRole(theRole) == false )

In the trade-off between redundancy and clarity, let's use a few more bits of your bandwidth to show the complete event handler, this time with the fetching from the ConfigurationManager done just once, at the top of the method.

protected void ddlStatus_DataBound(object sender, EventArgs e)
    string theRole = ConfigurationManager.
    string theStatus = ConfigurationManager.AppSettings

    if ( User.IsInRole(theRole) == false ) 
        // ddlStatus.Items.Remove("Closed");
        foreach (ListItem li in ddlStatus.Items)
            if ( li.Text.ToUpper() == theStatus.ToUpper() )

Finally, our third value is the status string to turn red, which is used in the BugReviewGrid_RowDataBound event handler in TBTReview.aspx.cs.

protected void BugReviewGrid_RowDataBound(object sender, 
    GridViewRowEventArgs e)
    string textToMatch =
    if (e.Row.RowType == DataControlRowType.DataRow)
        TableCell cell = e.Row.Cells[5];
        if (cell.Text.ToUpper() == textToMatch.ToUpper())
            cell.Font.Bold = true;
            cell.ForeColor = System.Drawing.Color.Red;

While this code may be a bit more cumbersome, I believe it is easier to maintain as it is crystal clear where these values are coming from (Web.config's AppSettings) and all of these "hard-coded" values are in a single place for easy maintenance.

[Footnote 1] This quote is rendered in various ways on the internet (and in hacker lore), all amounting to the same sentiment. WikiQuotes, however, gives the original as the somewhat more cumbersome "The supreme goal of all theory is to make the irreducible basic elements as simple and as few as possible without having to surrender the adequate representation of a single datum of experience."

Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.

Return to the Windows DevCenter.