I recently had to update some code with a new parameter that needed passing between the calling code and a series of lower level business and data classes. Over a period of years the classes have had more and more parameters added and adding another one meant changing 50+ objects plus all the calling code.
An example of this code is shown below
The Data Class being instantiated
Public Sub New(ByVal _DbType as common.DbType, ByVal _UserID as Integer, ByVal _Signature as String)
….
The calling code
Dim mDataObj as new myClass(common.DbType.SQLServer,myUserID, “John Ellis”)
So adding a new parameter was going to be time consuming and on the off chance any new parameters were going to be added in the future a better solution was required.
To resolve this I created a new Class to hold all the values that would be passed. It is important to consider which parameters are globally needed or you can end up moving them all into this class.
Public Class GlobalParams
Public mdbtype As iapCommon.Enums.DBType
Public mUserID As Integer = 0
Public mUserName As String = “”
Public mSignatureLine1 As String = “”
Public mSignatureLine2 As String = “”
Public Property DBType() As iapCommon.Enums.DBType
Get
Return mdbtype
End Get
Set(ByVal value As iapCommon.Enums.DBType)
mdbtype = value
End Set
End Property
Public Property UserID() As Integer
Get
Return mUserID
End Get
Set(ByVal value As Integer)
mUserID = value
End Set
End Property
Public Property UserName() As String
Get
Return mUserName
End Get
Set(ByVal value As String)
mUserName = value
End Set
End Property
Public Property SignatureLine1() As String
Get
Return mSignatureLine1
End Get
Set(ByVal value As String)
mSignatureLine1 = value
End Set
End Property
Public Property SignatureLine2() As String
Get
Return mSignatureLine2
End Get
Set(ByVal value As String)
mSignatureLine2 = value
End Set
End Property
End Class
This now changes the code above to
The Data Class being instantiated
Friend mGlobalParams as new common.GlobalParams
Public Sub New(ByVal _GlobalParams as common.GlobalParams)
mGlobalParams = _GlobalParams
The calling code
dim mGlobalParams as new common.GlobalParams
mGlobalParams.DbType = common.DbType.SQLServer,myUserID
mGlobalParams.UserId = 20088
mGlobalParams.Signature = “John Ellis”
Dim mDataObj as new myClass(mGlobalParams)
This may not seem a great improvement but now if we want a new global parameter we can add it to the GlobalParams class and we only have to change the code the requires the new parameter. This reduces maintenance also as the GlobalParams are usually set up in the load function of the main program they only need to be populated one and then each time the class is instantiated you are only coding the one line for the New.
Better solution, let me know.