"Marc R." <No****@NewgroupsONLY.com> wrote in message
news:eP**************@tk2msftngp13.phx.gbl...
Is there a more simple way to bind the "Service" nodes directly to the
table so I don't have to work with the Dataview ?
Certainly, but you just wind up with the code inside the Service class,
rather than anywhere else and you'll be starting to fight the battles of
encapsulation vs performance.
Some suggestions:
1) Leave things as they are, loading data into each Service object
from the DataView - OK, it's "messy", but it works.
At the very least, I recommend to code your own Constructor
so that you set all the properties in one go, as in
Public Class Service
. . .
Public Sub New(
ByVal id As Integer _
, ByVal name As String _
, ByVal active As Boolean _
)
' Load values into properties
p_ID = id
p_Name = name
p_Active = active
End Sub
.. . .
2) Keep the DataView (so your database access stays where it is),
and create each Service object based on, say, a row from the
DataView, something like
Public Class Service
Public Sub New( _
ByVal sourceData as DataViewRow _
)
' Load data from DataViewRow into properties
p_ID = sourceData.Item( "id" )
End Sub
.. . .
Both of the above are limited in that you're getting a /copy/ of the data
in each object. unless you move the database code itself into the Service
class, you can't feed changes [directly] back into the database, so ...
3) Transfer all the data access into the Service class as well.
You'd probably want to do much the same as in (2), but passing
in the database /connection/ instead of the already loaded DataView,
and probably the id of the Service to load.
Public Class Service
Public Sub New( _
ByVal id As Integer _
, ByVal connection As WhicheverConnectionYouUse _
)
' Save the connection, so we can use it do updates - maybe
p_Connection = connection
' Load data from DataViewRow into properties
p_ID = GoAndGetDataFromConnection( connection, id )
End Sub
.. . .
4) Or, because that'll be /horrendously/ slow cmpared to loading the
data from the DataView (because each Service object has to do its
own, /individual/ data retrieval), you might tackle it completely the
other way around ... Create a Shared method on the Service class
that takes a /TreeView/ as argument, finds all the Services (from the
Database), creates a Service object for each and adds these into the
TreeView. <whew>
Public Class Service
Public Shared Sub LoadTree( _
ByVal treeToLoad As TreeView _
, ByVal connection As WhicheverConnectionYouUse _
)
Dim dv As DataView = ... DataFromDatabase
For Each dr As DataRowView In dv
Dim node As Service _
= New Service( dr )
treeToLoad.Nodes.Add( node )
Next
End Sub
.. . .
(plus, of course, at least 101 others I /haven't/ thought of ... )
Personally, I think I'd be heading for option (2).
HTH,
Phill W.