Living .NET...

Musings on .NET, and the like - Manoj G [MVP, Connected Systems Developer]

Useful additions to .NET 2.0 - Part 2: Nullable Types

Nullable types, put in simple terms can be thought of as a way of having nullable value types. Quite obviously, one of the largest applicability of this concept is that of database programming, where you can associate a null value with any datatype. Not too long ago, while coding the data access layer, I did quite a lot of magic value checks on values to see whether I had to associate a null value (DBNull.Value) to the corresponding database parameter. This included Guid.Empty, Int32.MinValue, DateTime.MinValue and so on. So, I had to create one helper method for all the types that needed such checks. The example for int is shown below:
 
static object GetDbIntValue(int val)
{
    
if (val == int.MinValue)
    {
        
return DBNull.Value;
    }
    
else
    
{
        
return val;
    }
}
 
Now, this effort is not good in a lot of ways though it serves its purpose. This methodology would require a handshake between the Business Logic Layer (BLL) and the Data Access Layer (DAL) as to what values are acceptable and what are not. The problem is that there is no universally accepted set for the same, and hence there would be semantic coupling between the Biz and the DAL layers. The handsome alternative to this situation would be to use Nullable types. Wherever you used magic values, replace them with the corresponding instances of Nullable<T>. Most typically, these would be in the data transfer objects (DTO). In the DAL, we could now have a single helper function (in lieu of many in the earlier case), which looks something like:
 
static object GetDbValue<T>(Nullable<T> val) where T : struct 
{
    
if (val.HasValue)
    {
        
return val.Value;
    }
    
else 
    
{
        
return DBNull.Value;
    }
}
 
An adroit developer might question whether this helper nethod is really required. Why not have the IDbDataParameter instances 'infer' the right Dbtype from correponding nullable types. For example, you might have wanted this snippet to work without a hitch:
 
Guid? id = Guid.NewGuid(); //Just an example 
SqlCommand cmd = new SqlCommand("Select * from Sample where id = @Id");
cmd.Parameters.AddWithValue("@Id", id);

But unfortunately, at this point of time, there is no conversions built in for nullable types and you would end up getting this exception message:

No mapping exists from object type System.Nullable`1[[System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to a known managed provider native type.
 
So, till there is support for Nullable types from System.Data (not sure why it should not be), you are better off using the simple helper listed earlier.
 
Before closing off, I would recommend that you read this article to understand all aspects of Nullable types. 
 
Nullable types, unfortunately, is fully supported only in C# 2.0, and not in VB. In other words, VB does not have the additional language additions and compiler changes needed to support nullable types in a way C# does. However, nothing stops you from using Nullable<T> in VB; it is just that you would not have the shortcut notations like in C# (int? etc). You are encouraged to read this blog post by Paul Vick, who explains this in detail.
Posted: Sun, Nov 20 2005 12:14 by Manoj G | with no comments
Filed under:
Leave a Comment

(required) 

(required) 

(optional)

(required)