C
Confused Newbie
I'm converting an app written in VB 2003 to 2005 and need advice for how to
deal with this situation:
The app has a number of "manager" classes that handle the data access. They
all have several routines that are identical, except for the object type and
database action specific to that particular class, such as "Public ReadOnly
Property Item(ByVal SystemID As Integer) As CEmployee" or "Public Shared
Function Save(ByVal TheItem As CEmployee) As Boolean". Each of the object
classes also have a bunch of common properties and methods that are
inherited from an abstract base class.
What I want to do with the manager classes is rewrite them using an abstract
class with MustOverride properties and methods. Where I'm hung up is the
existing implementation uses an Enum that defines the database field names
and ordinal positions in the SELECT statement used to create the datareader.
For example:
Public Class EmployeeManager
Public Enum MyFields
NameSystemID = 0
NameFirst
NameMiddle
NameLast
DateHired
RecStatus
RecDateAdd
RecDateRev
RecAddBy
RecRevBy
End Enum
Private Shared _FieldList As ArrayList = Nothing
Public Shared ReadOnly Property FieldList() As ArrayList
Get
If _FieldList Is Nothing Then
_FieldList = New ArrayList
Dim arNames() As String
arNames = MyFields.GetNames(GetType(MyFields))
For x As Integer = 0 To arNames.GetUpperBound(0)
_FieldList.Add(New VisarEnumBindingItem(EnumValue:=x,
EnumLabel:=arNames(x))
Next
End If
Return _FieldList
End Get
End Property
Private Shared _FieldListString As String = ""
Public Shared ReadOnly Property FieldListString() As String
Get
If _FieldListString = "" Then
Dim ThisItem As VisarEnumBindingItem = Nothing
For x As Integer = 0 To FieldList.Count -1
ThisItem = CType(_FieldList(x), VisarEnumBindingItem)
_FieldListString += ThisItem.StringValue & ", "
Next
_FieldListString = Left(_FieldListString,
_FieldListString.LastIndexOf(", "))
End If
Return _FieldListString
End Get
End Property
' Overloaded Item property creates a command to retrieve by PK or AK,
passes command to ItemFetch, which in turn calls ItemFill to unpack the
datareader into the instance object and pass it back up the chain.
Private Shared Function ItemFill(ByVal ItemReader As OleDbDataReader) As
CEmployee
With ItemReader
Return New CEmployee(RecSystemID:=.GetInt32(MyFields.NameSystemID),
RecStatus:=VisarGoodies.MakeInt32(.GetValue(MyFields.RecStatus)),
RecDateAdd:=VisarGoodies.MakeDate(.GetValue(MyFields.RecDateAdd)),
RecDateRev:=VisarGoodies.MakeDate(.GetValue(MyFields.RecDateRev)),
RecAddBy:=VisarGoodies.MakeInt32(.GetValue(MyFields.RecAddBy)),
RecRevBy:=VisarGoodies.MakeInt32(.GetValue(MyFields.RecRevBy)),
NameFirst:=VisarGoodies.MakeString(.GetValue(MyFields.NameFirst)),
NameMiddle:=VisarGoodies.MakeString(.GetValue(MyFields.NameMiddle)),
NameLast:=VisarGoodies.MakeString(.GetValue(MyFields.NameLast)))
End With
End Function
End Class
The way it's designed, changes to the order of fields in the select
statement or even actual field names only need to be made in the Enum. The
select, insert, and update statements are dynamically constructed in each of
the managers from the FieldList arraylist.
How can I create the identical functionality using an abstract class in
VB2005, also placing the code that constructs the "FieldList" from the Enum
into a shared method in a separate utility class? I tried using a
placeholder enum and the FieldList property in the base class, and a
shadowed enum in a derived class, but when executed, it uses the base enum,
not the shadowed one. It does work when place the enum and override
FieldList in each of the derived classes but I don't want to implement
identical code over and over.
Thanks,
Gino
deal with this situation:
The app has a number of "manager" classes that handle the data access. They
all have several routines that are identical, except for the object type and
database action specific to that particular class, such as "Public ReadOnly
Property Item(ByVal SystemID As Integer) As CEmployee" or "Public Shared
Function Save(ByVal TheItem As CEmployee) As Boolean". Each of the object
classes also have a bunch of common properties and methods that are
inherited from an abstract base class.
What I want to do with the manager classes is rewrite them using an abstract
class with MustOverride properties and methods. Where I'm hung up is the
existing implementation uses an Enum that defines the database field names
and ordinal positions in the SELECT statement used to create the datareader.
For example:
Public Class EmployeeManager
Public Enum MyFields
NameSystemID = 0
NameFirst
NameMiddle
NameLast
DateHired
RecStatus
RecDateAdd
RecDateRev
RecAddBy
RecRevBy
End Enum
Private Shared _FieldList As ArrayList = Nothing
Public Shared ReadOnly Property FieldList() As ArrayList
Get
If _FieldList Is Nothing Then
_FieldList = New ArrayList
Dim arNames() As String
arNames = MyFields.GetNames(GetType(MyFields))
For x As Integer = 0 To arNames.GetUpperBound(0)
_FieldList.Add(New VisarEnumBindingItem(EnumValue:=x,
EnumLabel:=arNames(x))
Next
End If
Return _FieldList
End Get
End Property
Private Shared _FieldListString As String = ""
Public Shared ReadOnly Property FieldListString() As String
Get
If _FieldListString = "" Then
Dim ThisItem As VisarEnumBindingItem = Nothing
For x As Integer = 0 To FieldList.Count -1
ThisItem = CType(_FieldList(x), VisarEnumBindingItem)
_FieldListString += ThisItem.StringValue & ", "
Next
_FieldListString = Left(_FieldListString,
_FieldListString.LastIndexOf(", "))
End If
Return _FieldListString
End Get
End Property
' Overloaded Item property creates a command to retrieve by PK or AK,
passes command to ItemFetch, which in turn calls ItemFill to unpack the
datareader into the instance object and pass it back up the chain.
Private Shared Function ItemFill(ByVal ItemReader As OleDbDataReader) As
CEmployee
With ItemReader
Return New CEmployee(RecSystemID:=.GetInt32(MyFields.NameSystemID),
RecStatus:=VisarGoodies.MakeInt32(.GetValue(MyFields.RecStatus)),
RecDateAdd:=VisarGoodies.MakeDate(.GetValue(MyFields.RecDateAdd)),
RecDateRev:=VisarGoodies.MakeDate(.GetValue(MyFields.RecDateRev)),
RecAddBy:=VisarGoodies.MakeInt32(.GetValue(MyFields.RecAddBy)),
RecRevBy:=VisarGoodies.MakeInt32(.GetValue(MyFields.RecRevBy)),
NameFirst:=VisarGoodies.MakeString(.GetValue(MyFields.NameFirst)),
NameMiddle:=VisarGoodies.MakeString(.GetValue(MyFields.NameMiddle)),
NameLast:=VisarGoodies.MakeString(.GetValue(MyFields.NameLast)))
End With
End Function
End Class
The way it's designed, changes to the order of fields in the select
statement or even actual field names only need to be made in the Enum. The
select, insert, and update statements are dynamically constructed in each of
the managers from the FieldList arraylist.
How can I create the identical functionality using an abstract class in
VB2005, also placing the code that constructs the "FieldList" from the Enum
into a shared method in a separate utility class? I tried using a
placeholder enum and the FieldList property in the base class, and a
shadowed enum in a derived class, but when executed, it uses the base enum,
not the shadowed one. It does work when place the enum and override
FieldList in each of the derived classes but I don't want to implement
identical code over and over.
Thanks,
Gino