Category / Uncategorized

Business Layer/Data Layer architecture,Test Driven Development and MVC May 18, 2009 at 7:54 pm

Using Test Driven Development (TDD) with ASP.NET can be a messy process if your not aware of some pitfalls.  Say you wrote a button click event handler that does a couple of string conversions before calling the business layer: 

protected void Button1_Click(object sender, EventArgs e)
{
     string aString =     //Do some string conversions
     BusinessLayer bl = new BusinessLayer(aString);

     string response = bl.GetSomeString();
     Response.Write(response);

}

You also have a unit test for the Business Layer’s GetSomeString method:
{
   EnsureReturnIsNotEmpty()
   {
        BusinessLayer bl = new BusinessLayer();
        string response = bl.GetSomeString();
         Debugger.Assert(response == “”);

    }

}
In this scenario, it is possible that your unit test will pass even when the user interface fails.  If a string conversion in the ASP.NET page breaks (See the button_click event handler) this will not be tested by the unit test.

To fix this issue you can move the code (//Do some string conversions) to the business layer.  You are still left w/ code in the Button Click Method (E.G. Response.Write()).  This does not provide the clean seperation of layers that is offered by the MVC design pattern, therefore TDD cannot be fully entrusted to ensure your code will run.  Though, if you keep the code in the ASP.NET code behind pages to a minimum.  Your problems beyond the help of TDD should remain at a minimum as well.  Perhaps Microsoft’s new ASP.NET MVC will solve this issue:  http://www.asp.net/mvc/whatisaspmvc/

XML Serialization with FusionCharts in ASP.NET April 30, 2009 at 9:36 pm

FusionCharts offers a set of ASP.NET compatible Charts that one can download for development and testing.  They have great documentation on their site so I’m not getting into that. 

I wrote a program that lets you populate the chart easily with your own data.  Download the sample and go to the code behind for Default.aspx.cs line 33:

//Set your custom data output here
string[] Labels = { “Jan”, “Feb”, “March” };
string[] Values = { “174″, “18100″, “20000″ };
int count = 0;
setItems[] setItems = new setItems[Labels.Length];
foreach (string lbl in Labels)
{
setItems[count] = new setItems();
setItems[count].Lbl = Labels[count];
setItems[count].Val = Values[count];
count++;
}

You can add the rest of the months of the year and your own data to display or you can totally change the date range and type of data by changing the other options of the class:

chart.Caption = “Monthly Sales Summary”; //Customizing the caption

The project is made in Visual Studio 2008 and should work as is save 1 detail:  go into the Web.config and change the following key:
<add key=”DataDirectory” value=”C:\Users\Brian\Documents\Visual Studio 2008\Projects\FusionChartSerializer\FusionChartSerializer\Data” />
To point to wherever your Data File is (the Data folder within the FusionChartSerializer project).