So here is the code:
return new DistrictInfo {
rid = Convert.ToUInt32(((OracleNumber)o.GetOracleValue("rid")).Value),
doc = Convert.ToUInt16(((OracleNumber)o.GetOracleValue("doctor")).Value),
secdoc = Convert.ToUInt16(((OracleNumber)o.GetOracleValue("secdoctor")).Value),
num = Convert.ToUInt16(((OracleNumber)o.GetOracleValue("num")).Value),
docname = o.GetOracleValue("doctorname") as string,
slpuname = o.GetOracleValue("lpuname") as string,
reason = o.GetOracleValue("reason") as string,
secdocname = o.GetOracleValue("secdocname") as string
};
Now I need to rewrite this code to do a checking whether object property is existing or not. This should be like this piece of code:
DistrictInfo di;
if (!(o["rid"].Equals(DBNull.Value)) && !(o.GetOracleValue("rid").Equals(DBNull.Value)) && (((OracleNumber)o.GetOracleValue("rid")).Value != null))
{
di.rid = Convert.ToUInt32(((OracleNumber)o.GetOracleValue("rid")).Value);
}
But I found this code some kind of awkward and not elegant. I did a many of checking ’cause I want to escape exceptions.
So the question is how can we refactor this code? Tell me your thoughts. I think there’s no need in such many checkings. Another thing we need to specify a variety of object property names to do one block of code for all variety members. I think there’s ability to use LINQ for that. Also in first piece of code we see different casting so we need to mention it in our new code.
Thanks in advance guys!
P.S. Library for working with database is dotConnect for Oracle from devArt.
I would suggest you to use UInt32.TryParse() which provides a safe method of converting raw string value to
UInt32, there are two cases – value can be converted to UInt32 or can not. So basically check return value ofTryParse()method to see whether value was converted successfully.So
Regarding automation of this process to handle multiple properties by their names you also need to consider property type, so I can not see a nice way to leverage LINQ for this purpose.
EDIT: Added proposal regarding automation of a field transformation
You can leverage Extension Methods feature of
.NETto decorateOracleObjecttype by a set of helpful methods.EDIT2: Extension Methods description