Click here to Skip to main content
15,894,252 members
Articles / Programming Languages / Visual Basic

Refactoring - elixir of youth for legacy VB code

Rate me:
Please Sign up or sign in to vote.
4.85/5 (45 votes)
3 Dec 2004CPOL22 min read 146.6K   307   73  
Standard refactoring techniques coupled with automated refactoring tool provide excellent platform for legacy VB code upgrade. Legacy VB code suffers poor structure and bloated code due to lack of inheritance and other OO capabilities.
Imports ADOR

Namespace TeslaTeam.RefactoringVB.RefactoredECCFramework

    Public MustInherit Class EntityPersistence

        Inherits SecuredObject

        Private mvarSecurityToken As String
        Protected mcol As EntityCollection

        Public Sub New(ByRef col As EntityCollection)
            mcol = col
        End Sub

        Protected MustOverride Function StoredProcInsert() As String
        Protected MustOverride Sub UpdateCollectionWithReturnedData(ByVal rsAccount As Recordset, ByRef entity As Entity)
        Protected MustOverride Function StoredProcUpdate() As String
        Protected MustOverride Function PrepareUpdateData(ByVal eccentity As Entity, ByVal vParameters As Object) As Object
        Protected MustOverride Function PrepareDeleteData(ByVal oEntity As Entity, ByVal vParameters As Object) As Object

        Protected MustOverride Function StoredProcDelete() As String

        Public Function Delete() As Boolean
            Dim LowerLimit As Integer
            Dim UpperLimit As Integer
            LowerLimit = 1
            UpperLimit = mcol.Count
            Delete = Delete(LowerLimit, UpperLimit)
        End Function

        ' This method deletes items from the database. If the Index Key is not passed in
        ' then it delete everything. If it is passed in then it will just deletes that
        ' record. Returns True or False.
        Public Function Delete(ByVal Index As Integer) As Boolean
            Dim LowerLimit As Integer
            Dim UpperLimit As Integer
            If Index < 1 Or Index > mcol.Count Then
                Err.Raise(ERR_ACCOUNTINDEXOUTOFRANGE, "EntityCollection.Delete PROC", "Index out of range")
            Else
                LowerLimit = Index
                UpperLimit = Index
            End If
            Delete = Delete(LowerLimit, UpperLimit)
        End Function

        Private Function Delete(ByRef LowerLimit As Integer, ByRef UpperLimit As Integer) As Object
            Dim inx As Integer
            Dim ErrorNum As Integer
            Dim oEntity As Entity
            Dim vParameters(0, 0) As Object
            Dim SPName As String

            Dim oDALEng As IOBPDA.IOBPConnection
            If mcol.Count = 0 Then 'Nothing to Update
                Delete = True
            End If
            If Not SafeCreateObject(oDALEng, OBP_DA_CONNECTION, ErrorNum) Then
                Err.Raise(ERR_ACCOUNTSAFECREATEFAILED, "EntityCollection.Delete PROC", "Unable to create " & OBP_DA_CONNECTION & ". Return Code was: " & ErrorNum)
            End If

            For inx = UpperLimit To LowerLimit Step -1

                oEntity = mcol.Item(inx)

                If Not oEntity.IsNew Then

                    vParameters = PrepareDeleteData(oEntity, vParameters)

                    If Not oDALEng.Execute(SecurityToken, StoredProcDelete, vParameters) Then
                        Err.Raise(ERR_ACCOUNTDALDELETEFAILED, "EntityCollection.Delete PROC", "Delete Failed. SPName was: " & SP_D_ACCOUNT)
                    End If
                    Erase vParameters
                End If

                'Remove from Collection
                mcol.Remove((inx))
            Next inx
            Delete = True
        End Function

        ' This method persists data to the database if it is marked Dirty. If Index is
        ' passed in then update just that record or else update everything. Returns True
        ' or False.
        Public Function Update(ByVal Index As Integer) As Boolean
            Dim LowerLimit As Integer
            Dim UpperLimit As Integer
            If Index < 1 Or Index > mcol.Count Then
                Err.Raise(ERR_ACCOUNTINDEXOUTOFRANGE, "EntityCollection.Update PROC", "Index out of range")
            Else
                LowerLimit = Index
                UpperLimit = Index
            End If
        End Function

        Public Function Update() As Boolean
            Dim LowerLimit As Integer
            Dim UpperLimit As Integer
            LowerLimit = 1
            UpperLimit = mcol.Count
            Update = Update(LowerLimit, UpperLimit)
        End Function

        Private Function Update(ByRef LowerLimit As Integer, ByRef UpperLimit As Integer)

            Dim ErrorNum As Integer
            Dim oDALEng As IOBPDA.IOBPConnection
            Dim rsAccount As ADOR.Recordset
            Dim oEntity As Entity
            Dim vParameters(0, 0) As Object

            Dim inx As Integer
            Dim SPName As String

            Update = False

            If mcol.Count = 0 Then 'Nothing to Update
                Update = True
            End If

            If Not SafeCreateObject(oDALEng, OBP_DA_CONNECTION, ErrorNum) Then
                Err.Raise(ERR_ACCOUNTSAFECREATEFAILED, "EntityCollection.Update PROC", "Unable to create " & OBP_DA_CONNECTION & ". Return Code was: " & ErrorNum)
            End If

            For inx = LowerLimit To UpperLimit
                oEntity = mcol.Item(inx)
                If oEntity.Dirty Then

                    vParameters = PrepareUpdateData(oEntity, vParameters)

                    'Check to see if updating existing record to database
                    If oEntity.IsNew = False Then
                        'Update the record
                        SPName = StoredProcUpdate()

                    Else
                        'Inserting the record
                        SPName = StoredProcInsert()
                    End If

                    If Not oDALEng.Execute(SecurityToken, SPName, vParameters, rsAccount) Then
                        Err.Raise(ERR_ACCOUNTDALUPDATEFAILED, "EntityCollection.Update PROC", "Update to Database Failed. SPName was: " & SPName)
                    ElseIf (Not rsAccount Is Nothing) Then  'Set to True if nothing returned in DB
                        If rsAccount.RecordCount > 1 Then
                            Err.Raise(ERR_ACCOUNTUPDATEFAILEDRETURNTOOMANYRECORDS, "EntityCollection.Update PROC", "Update to Database Failed. Returned more than one record. SPName was: " & SPName)
                        Else
                            UpdateCollectionWithReturnedData(rsAccount, oEntity)
                        End If
                    End If
                End If 'Dirty
            Next inx
            Update = True
            Erase vParameters
        End Function

    End Class

End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Chile Chile
Danijel Arsenovski is senior developer and consultant from Santiago, Chile. His interests include advanced OO programming techniques and refactoring. He holds Microsoft's Solution Developer Certification and is often speaker at Microsoft's technical conferences. Recently, he has been recognized as Microsoft MVP.
He is the author of book "Professional Refactoring in Visual Basic" and "Professional Refactoring in C# and ASP .NET" from Wrox.
From time to time he blogs at http://blog.refactoringin.net

Comments and Discussions