I have some (a bit more than a little) code where object values from a DataReader are assigned to business object properties. This code is quite untidy to look at with lots of null coalescing, parsing, etc. I was wondering how to implement a tidier means of converting the object values from the DataReader to the appropriate values for the object properties.
One option is to have discrete conversion functions for each data type, where the data types on my object properties are limited to a small set of CLR primitive types. I don't use nullable types, as then I'll just be moving the untidiness to where these values are used elsewhere.
My second option is to create an extension method for each of the four or so data types used, limited to the niche namespace in which these assignments take place. Something like this rather simplistic example:
public static void SafelyAssign(this string target, object value)
{
if (value is string)
{
target = (string)value ?? "";
}
else
{
target = value.ToString();
}
}
What other options do I have?