Geeks With Blogs
Scott Van Vliet Once a Developer, Always a Developer

First of all -- I'm a total liar.  I was supposed to offer up some great insight from Tech•Ed.  But alas, I had to leave early and fly to Seattle for a meeting.  So, I wasn't able to share any fun stuff :(

We'll, I actually have a tiny bit of time to write, and I thought I'd share the result of recent code review.

I'm currently teaching .NET (Framework fundamentals, C#, et al.) to some of my clients who are preparing to take over the support of a very large application (we're going on 2 years of development and deployment!) As we were looking at some of the code, I came across some stuff from our offshore team that slipped by my team's radar. In this particular project, we found hundreds of lines that looked like this:

theatre.ID = (reader["theatre_id"] is DBNull) ?
   
0 : Convert.ToInt32(reader["theatre_id"]);

theatre.Code = (reader["theatre_code"] is DBNull) ?
   
String.Empty : reader["theatre_code"].ToString();

Ick!  I was very bummed to come across this while teaching my clients about best practice -- was a bit 'the fool'.  Not wanting to let this pass, I quickly redirected the discussion to talk about refactoring and reuse (which was kind of a nice segue to some OO topics.)  In this discussion, we wrote an Extension Method to the IDataReader class to clean-up this code.  The new code looked pretty:

theatre.ID = reader.GetValueOrDefault<int>("theatre_id");

theatre.Code = reader.GetValueOrDefault<string>("theatre_code");

The details of this Extension Method are as follows:

/// <SUMMARY>
/// Contains extension methods for the IDataReader interface.
/// </SUMMARY>
public static class DataReaderExtender
{
  /// <SUMMARY>
  /// This method will return the value of the specified columnName, cast to
  /// the type specified in T. However, if the value found in the reader is
  /// DBNull, this method will return the default value of the type T.
  /// </SUMMARY>
  /// <TYPEPARAM name="T">The type to which the value found in the reader should be cast.</TYPEPARAM>
  /// <PARAM name="reader">The reader in which columnName is found.</PARAM>
  /// <PARAM name="columnName">The columnName to retrieve.</PARAM>
  /// <RETURNS>The column value within the reader typed as T.</RETURNS>
 
public static T GetValueOrDefault<T>(this IDataReader reader, string columnName)
  {
   
object columnValue = reader[columnName];
    T returnValue = default(T);
    if (!(columnValue is DBNull))
    {
     
returnValue = (T)Convert.ChangeType(columnValue, typeof(T));
    }
    return returnValue; 
  
}
}

Enjoy! (I've posted the code here: DataReaderExtender.zip)

Posted on Wednesday, June 18, 2008 11:55 PM C# , Extension Methods | Back to top


Comments on this post: IDataReader Extension Methods

# re: IDataReader Extension Methods
Requesting Gravatar...
T. Ferg --

Yeah, there are still applications that have been written without NHibernate -- and many good ones. The question I'd ask is rather -- how about LLBLGen or ADO.NET Entity Framework vs. NHibernate...

Furthermore, when you think of the size/scale of the application in question -- development started in 2005 and is still underway (with ~1.5MM lines of code, between C# and PL/SQL, and not to say that metric is good or valid for anything, it just speaks to the shear volume of this bloody app.)

Rob --

Bravo on the Lambda, looks pretty sexy.

If you download the attached file, there is an overloaded method that includes the ordinal (by using GetName(int ordinal) and passing that to the other method.)

Cheers,

Scott
Left by Scott Van Vliet on Jun 19, 2008 5:50 AM

# re: IDataReader Extension Methods
Requesting Gravatar...
Paul --

Good catch. My team actually has several more overloads on this method, including one that has an additional parameter of type T that allows you to specify the desired "default value". In this case, you could call the code as follows:

theatre_code = reader.GetValueOrDefault<string>("theatre_code", String.Empty);

Cheers,

Scott
Left by Scott Van Vliet on Jun 19, 2008 2:34 PM

# re: IDataReader Extension Methods
Requesting Gravatar...
I was just searching for about this when I discovered your blog post. I’m only visiting to say that I truly enjoyed reading this post, it’s really well written. Are you going to blog more on this? It looks like there’s more material here for more posts.
Left by chi flat iron on Sep 04, 2009 9:42 AM

# re: IDataReader Extension Methods
Requesting Gravatar...
I have a store provide chi Hair Straightener,with best quality cheap price,if you’er interested in it,you can have a look of it.
Left by chi Hair Straightener on Jul 05, 2010 8:51 PM

# re: IDataReader Extension Methods
Requesting Gravatar...
Thanks for the idea, this is a relevant information now. I'll be using this process. - Fred Wehba
Left by Fred on Sep 14, 2015 6:47 PM

Your comment:
 (will show your gravatar)


Copyright © svanvliet | Powered by: GeeksWithBlogs.net