"deserialization" using DataRow - suggestions please

  • Thread starter Thread starter pokémon
  • Start date Start date
P

pokémon

In my constructors, I am passing in a DataRow object, then setting the
members based on the field values. For example:

class MyClass {
int member1 = 0;
public MyClass(DataRow dr) {
this.member1 = (int)dr["FIELD1"];
}
}

I have to do this for potentially a lot of objects.

I am wondering, is there a better approach to achiving this functionality?

Suggestions/Critique welcome. Thanks in advance.

-KJ
 
If you look at the code that a typed DataSet generates for its column value
members, this is how it's done.

I think that when the 2.0 version of the CLR comes out next year, and we
have generics, there will be cleaner and more performant ways to do this
kind of thing, but for now -- at some point, you have to do the cast.

The only optimization I can think of is that dr[0], for instance, would be
faster than dr["FIELD1"], so if you will have to read multiple records from
the same table you might wring out a little more performance by using
indexes instead of names. The trade-off is that your code would then become
sensitive to schema changes in the table that might reorder the columns.

--Bob
 
Back
Top