When we use data readers in our data access layers of a c# application that could be a part of web or windows, we have to handle nullable parameters and nullable values before casting or accessing to avoid any run time errors or basically to avoid crashing the application. So we can write additional reader extension to use that can handle null values for a nullable property. For example see the below method.
Many data types are covered with this method and this could be copied to your base class of data access layer which then be accessible as a static method to any data layer methods. Look at the below example.
This is a peace of code of a data access method that the reader is a SqlDataReader and the string value of lbActionType can also be nullable. So directly using GetString may throw null reference if you don't handle it in reader object. So the "GetStringNullable" will save lot of your time and additional coding.
Get this code and try it yourself. It is easy as say cheese. :)\
Remember the saying,
“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.” ― Bill Gates
It is not being lazy sometimes, it is the way of finding alternatives. :)
public static class DataReaderExtensions
{
public static string GetStringNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? null : reader.GetString(ordinal);
}
public static int? GetInt32Nullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (int?)null : reader.GetInt32(ordinal);
}
public static DateTime? GetDateTimeNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (DateTime?)null : reader.GetDateTime(ordinal);
}
public static decimal? GetDecimalNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (decimal?)null : reader.GetDecimal(ordinal);
}
public static bool? GetBooleanNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (bool?)null : reader.GetBoolean(ordinal);
}
public static byte? GetByteNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (byte?)null : reader.GetByte(ordinal);
}
public static double? GetDoubleNullable(this IDataReader reader, int ordinal)
{
return reader.IsDBNull(ordinal) ? (double?)null : reader.GetDouble(ordinal);
}
}
Many data types are covered with this method and this could be copied to your base class of data access layer which then be accessible as a static method to any data layer methods. Look at the below example.
while (reader.Read())
{
AdmActionEntity action = new AdmActionEntity();
action.idActionType = action.ActionType.idActionType = reader.GetInt32(idActionTypeOrdinal);
action.ActionType.lbActionType = reader.GetStringNullable(lbActionTypeOrdinal);
action.nbToBeAssigned = reader.GetInt32(nbToBeAssignedOrdinal);
action.bnToBeDone = reader.GetInt32(bnToBeDoneOrdinal);
ActionList.Add(action);
}
This is a peace of code of a data access method that the reader is a SqlDataReader and the string value of lbActionType can also be nullable. So directly using GetString may throw null reference if you don't handle it in reader object. So the "GetStringNullable" will save lot of your time and additional coding.
Get this code and try it yourself. It is easy as say cheese. :)\
Remember the saying,
“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.” ― Bill Gates
It is not being lazy sometimes, it is the way of finding alternatives. :)
No comments:
Post a Comment