SafeDataReader (default values)

SafeDataReader (default values)

Old forum URL: forums.lhotka.net/forums/t/604.aspx


rbraun posted on Wednesday, July 12, 2006

I have a question regarding default values in the SafeDataReader that I was hoping to get help with (or at least point me in the right direction).

Unfortunately, when we get data from our external source, it is not guaranteed to populate all columns in our tables. So this means in our table design we can't use the 'not null' option in our column definition. This also means that we might or might not have 'real' data when our objects are created. The problem is that when we call our SafeDataReader.GetInt32 for instance (this also applies to GetBoolean(), GetDouble(), etc), we may get a value because of the null value in the column but this might or might not be valid data.

To get around this, I created a simple class to with two properties, HasData (bool) and Value (contains real value). The HasData property is set after determining !IsDbNull for that column value. So before using the data, we ask if it HasData before proceeding. This solution seems a bit sketchy, but at least gives us an indicator if the value is real or not. Does anyone have a better solution for this?


SafeDataReader.cs---------------------------------
...
    public virtual int GetInt32(int i)
    {
      if (_dataReader.IsDBNull(i))
        return 0;
      else
        return _dataReader.GetInt32(i);
    }
...

 

 

 

ajj3085 replied on Wednesday, July 12, 2006

My only comment is that you may want to do this outside the reader, and have the BO 'figure out' if the value is null.  The reason is that your default may vary in different BOs.  If  you embed the choosing of the default in the SafeDataReader, you're locking yourself into saying that a particular data type is ALWAYS doing to default to that value.

Copyright (c) Marimer LLC