Problem Solved

Last week I blogged about a problem I was having finding common ground between the DataReader object, and the Datarow object. I complained of having to write two different constructors that were identical in nature except for the type of object received. Why the solution didn't occur to me before now I can't say, but the answer is the GoF Adapter pattern.

I just wrote an object that adapts the DataRow to the IDataRecord Interface. Problem solved. Simple.

There were some methods that I couldn't completely implement, such as the one that referenced the parent IDataReader object (there isn't one in this scenario), but it should work find for my purposes.

<source>

Public Class DataRowAdapter

Implements System.Data.IDataRecord

Private _row As DataRow

Protected ReadOnly Property Row() As DataRow

Get

Return Me._row

End Get

End Property

#Region "IDataRecord"

... snipped

Default Public Overloads ReadOnly Property Item(ByVal i As Integer) As Object Implements System.Data.IDataRecord.Item

Get

Return Row(i)

End Get

End Property

Default Public Overloads ReadOnly Property Item(ByVal name As String) As Object Implements System.Data.IDataRecord.Item

Get

Return Row(name)

End Get

End Property

#End Region

Public Sub New(ByVal row As DataRow)

Me._row = row

End Sub

End Class

</source>

Comments

# re: Problem Solved

Tuesday, August 02, 2005 8:32 AM by Jeffrey Palermo

Why would you ever want to pass an ADO object to a constructor? I know you are not talking about a domain object, but even your factory or builder objects. Passing a DataReader can be very bad since it is created somewhere and then passed off. The connection to your datastore is open the whole time. A better approach would be for your repository object to have a builder method that gets the ADO object, extracts the info and builds (constructs) your object. Then the complete instance can be returned.

# re: Problem Solved

Tuesday, August 02, 2005 8:48 AM by Chris McKenzie

Good point--but that's sort of what I'm doing. My FACTORY objects simply return Interfaces. The implementation objects are declared as Friend so they cannot be accessed directly by external callers.

The code looks something like:

Public function GetMyObject() as IMyObject
' Code to build an IDataRecord Object.

dim myObj as IMyObject = New MyObject(MyIDataRecord)
Return myObj

End Function

# DataRow adaptation to IDataRecord &laquo; Carl&#8217;s Geek Notes

Wednesday, March 21, 2007 2:07 PM by DataRow adaptation to IDataRecord « Carl’s Geek Notes

Leave a Comment

(required) 
(required) 
(optional)
(required)