I am writing code that reads data from a DB, where boolean values are stored as 0/1 nullable integers.
I decided to write a simple function to encapsulate the logic which converts the integer values to boolean
. Thing is, I sometimes need to regard the db null
value as boolean
false, and sometimes just as null
.
So I've come up with the following two extension methods:
// Read the specified column value from row as nullable boolean,
// treating integer 0 as false, and any other non-null value as true.
public static bool? ReadAsBoolNullable(this DataRow row, string columnName)
{
if (row.IsNull(columnName))
{
return null;
}
else
{
return row[columnName] != 0;
}
}
// Read the specified column value from row as boolean,
// treating null and integer 0 as false, and any other value as true.
public static bool ReadAsBool(this DataRow row, string columnName)
{
bool? data = row.ReadAsBoolNullable(columnName);
return data.HasValue ? data.Value : false;
}
My main concern about this code is that I potentially have hundreds of millions of records, each having up to 10 boolean (not nullable) columns. So the second function will be executed a lot and will have to call the first one a lot. Could this damage performance significantly, given that the function itself doesn't do much?