Wednesday, 14 May 2008

Using Generics To Check For Null In An SQL Reader

After a break from writing data access code, I was back onto it yesterday. The code I was writing was retrieving a contact. The contact object was being populated by retrieving value from an SqlDataReader. However a null value was being returned from the SqlDataReader.

The code samples below document how I moved from my intial code that didn't check for null to a method that allows me to check for null and return a value of any type using generics.

Code example 1:



this.Address2 = reader.GetString(reader.GetOrdinal("Address1"));

Code example 2:



int ordinal = reader.GetOrdinal("Address1");
if (!reader.IsDBNull(ordinal))
{
this.Address1 = reader.GetString(ordinal);
}
else
{
this.Address1 = "";
}

Code example 3:



private static string GetStringReaderColumn(SqlDataReader reader, string columnName, string defaultValue)
{
int ordinal = reader.GetOrdinal(columnName);
if (!reader.IsDBNull(ordinal))
{
return reader.GetString(ordinal);
}
else
{
return defaultValue;
}
}

Code example 4:


private static int GetInt32ReaderColumn(SqlDataReader reader, string columnName, int defaultValue)
{
int ordinal = reader.GetOrdinal(columnName);
if (!reader.IsDBNull(ordinal))
{
return reader.GetInt32(ordinal);
}
else
{
return defaultValue;
}
}

Code Example 5:


private delegate T GetReaderColumnDelegate(int ordinal);

private static T GetReaderColumn(SqlDataReader reader, string columnName, T defaultValue, GetReaderColumnDelegate getReaderColumnDelegate)
{
int ordinal = reader.GetOrdinal(columnName);
if (!reader.IsDBNull(ordinal))
{
return getReaderColumnDelegate(ordinal);
}
else
{
return defaultValue;
}
}

Code Example 6:



private static string GetStringReaderColumn(SqlDataReader reader, string columnName, string defaultValue)
{
return GetReaderColumn(reader, columnName, defaultValue, reader.GetString);
}

private static int GetInt32ReaderColumn(SqlDataReader reader, string columnName, int defaultValue)
{
return GetReaderColumn(reader, columnName, defaultValue, reader.GetInt32);
}

Saturday, 12 April 2008

Unit Testing the UI of Web Apps

Over the past 4 months I've been using the awesome tool Webaii, by Art of Test, to unit test my web UI work.

It works with both NUnit and the visual studio unit test tool.

I'll try and post some examples in the future of how I use it to unit test web pages.

Go to http://www.artoftest.com/ and download it now.

Wednesday, 26 March 2008

Unit Testing Code Coverage In Action

I just experienced the usefulness of unit testing at work today. I was making some changes to a few objects and ran all the tests for these objects prior to changing the code. A couple of the tests failed. After investigating I discovered that some of the column names in a database table had been changed and the stored procedures updated on the live DB but not the Dev DB. Without the coverage of the unit tests I may well have updated the live stored procedures with those on the Dev machine.

Tuesday, 4 March 2008

What I learnt today

To unit test or not to unit test?

I've spent a week making what I thought was a simple change to the site I'm working on. I predicted it would take no longer than a few days. I had to take some code on 4 pages, place them in controls and then put them on another set of pages.

I decided to unit test (or integration test) all the front end stuff using webaii (http://www.artoftest.com). This however has proved to more than double the amount of time it has taken to complete the task as I need to write all the tests (there weren't written when the code was created). Subsequently my superiors have started prodding me and asking me to speed up. With this added pressure I was thinking of dropping unit tests for anything that will be used within the office and only unit testing stuff viewed by the World wide web.

I'm now at the point where I need to change the controls so they work on the current pages and the new pages they'll be placed on. This is where my tests have now paid off as I'm making substantial changes to the code - which I can make with the confidence, that I won't break anything.

In conclusion I'm certain that unit tests should be used everywhere, but I think unless you have your fellow developers on board, so you're not writing tests for existing code, a comprise needs to be found where you write tests when you can but still get work out on time.

Tuesday, 5 February 2008

Expressing Intention

I just coded this method,private string GetSelectedSitesRowClass(int rowIndex) {...}which I'd used a couple of times. However I was getting confused as to if the rowIndex was zero based or not.

I thought 'lets comment it like this':/// <summary>
///
/// </summary>
/// <param name="rowIndex">Zero based row index</param>
/// <returns></returns>
private string GetSelectedSitesRowClass(int rowIndex) {...}
However remembering that its better to let the code express the intention of what something does, rather than a comment, I changed it to this:private string GetSelectedSitesRowClass(int zeroBasedRowIndex)I think this is better as there less code to confuse matters and the parameter names tells us exactly what it represents.

Thursday, 10 January 2008

Precision of c# datetime and the sql datetime

'tother day I was faced with a problem where two datetime values, one retrieved from a MS SQL Server database the other generated in c#, were unequal to each other even though when I displayed them, they both seemed to be the same.

After googling for good few minutes I discovered that the precision of the c# datetime type and the MS SQL Server datetime are radically different.

Basically c# measure the date and time down to 7 fractions of a second, whereas SQL measures the date and time down to 3.33 milliseconds.

To find out more about the sql server datetime type go to:
http://msdn2.microsoft.com/en-us/library/ms187819.aspx

Wednesday, 9 January 2008

Passing around null objects

I found this short, but good, article about using null within your code.

http://www.artima.com/weblogs/viewpost.jsp?thread=168511

I've made the mistake myself. Time to beat myself. Still lesson learn't.