Removing some of DataRow's ugliness through .NET 4.0’s dynamic
If you do test your stuff, not having static compilation at all times doesn’t seem too daunting. In such cases (and when you can use .NET 4.0) you can consider using the dynamic capabilities to give yourself a somewhat nicer API to deal with a DataRow (something you may end up with if you don’t want to take on a dependency to_ insert-your-favourite-ORM-tool-here_ for insert-whatever-reasons-you-have-here).
First we need the type construction to transpose from DataRow to dynamic:
The DataRowReader inherits from DynamicObject, which allows us to react to runtime calls to methods, properties, etc. we have NOT defined. For example, when somebody accesses a property which does not exist…
…or tries to write to a property…
…or calls some method on it…
What you can do now is the following:
Sure, the current implementation is pretty crude and can be foiled easily, but you get the idea.
If you think this further, you may arrive to a point that you consider accessing a DB dynamically in much the same fashion, something like…
Without ever having to generate code, set up mappings or dance 3 times around the grand DBA Master totem. Luckily, this is already happening.