I want to revisit a discussion that I had early on in my blog - Datasets.
After several months of heavy development (actually a conversion of some ASP code that I have been maintaining), I have found that I don't use the dataset.
Maybe it's the advice that I've been getting or maybe it's the result of my design. FWIW, here's what my stuff looks like (in VB):
' The following class will handle all the SQL Calls for an area of the site
Class sqlPortionOfSite
inherits sqlBaseClass
Public Function GetData() as CollectionOfMyData
Dim __dr as sqlDataReader
Dim __Rtn as CollectionOfMyData
__Rtn = New CollectionOfMyData()
Try
__dr = sqlHelper.ExecuteDataReader(ConnString, storedproc, "MySP") ' I use the Data Application Block
While __dr.read()
Dim __Data as MyData = New MyData
__Data.Field1 = __Dr.GetInteger(__Dr.getOrdinal("Field1"))
__Data.Field2 = __Dr.GetString(__Dr.getOrdinal("Field2"))
__Rtn.Add(__Data)
End While
Return __Rtn
Finally
If Not(__Dr is Nothing) AndAlso __Dr.isClosed() = false then __Dr.Close
End Try
End Class
So in this case I would have a class called MyData
Class MyData
Protected _Field1 as Integer
Public Property Field1 as Integer
Get
Return _Field1
End Get
Set (Value As Integer)
_Field1 = Value
End Set
End Property
Protected _Field2 as String
Public Property Field2 as String
Get
Return _Field2
End Get
Set (Value As Integer)
_Field1 = Value
End Set
End Property
End Class
and a collection class called CollectionOfMyData (which is way more complicated than I really want to document here... suffice to say start with a CollectionBase (or if Rob would publish his Collections there would be something even better).
I really like this method because I can easily pass around the individual row all over the place (in session, viewstate as well as in a list) and the resulting object is fairly light weight. When it comes to sorting (which was my problem before), I use Rob Teixeira's collection classes that he built here at work (I really wish he would release them); they have some built in methods for easily getting sorting/filtering to work.
The other thing you may notice is that I have separated my data access from the main part of the program (I actually have these classes in another project all together)... it forces me to do more work if a need to add a new field, but more than likely I would have to do something to display the field anyway. The other thing is that if there is a problem with the communication with the database, I know exactly where in the code the problem is.
Print | posted on Friday, May 28, 2004 10:26 AM