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);
}
...
Copyright (c) Marimer LLC