Currently, I am using a SqlDataReader
loaded up with a simple database 'Select' query. Each row represents a domain object, so I inflate each object like this:
Dim loadedItems As New List(Of Item)
Dim dr As SqlDataReader = GetItemsDataReader()
While dr.Read()
Dim item As Item = GetItemFromData(dr)
loadedItems.Add(item)
End While
The GetItemFromData
method I wrote looks something like this:
Private Function GetItemFromData(dr As SqlDataReader) As Item
Dim loadedItem As New Item()
loadedItem.ID = dr("ID")
loadedItem.Name = dr("Name")
'etc., etc.'
Return loadedItem
End Function
In some cases, I have to read data from a DataRow
instead of a SqlDataReader
. But the code would be the exact same! As I look at my GetItemFromData
method, I want to accept a more generic type of object in the dr
parameter so that I can treat a DataReader
the same as a DataRow
, since I would be writing the exact same code inside the method if I wrote one that was meant to use a DataRow
. Is there a way to do that?
Only way I can think of off the top of my head would be to wrap a few classes and implement an inferface - something like:
You would then have to wrap your row or reader before passing it to your method:
How about using a
DataTable
instead:Change your GetItemFromData() method to take a
DataRow
instead of aDataReader
(sorry about the VB.NET/C# hybrid pseudo code).