Click here to Skip to main content
15,895,709 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.7K   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 Factory

        Public Enum DeleteRecordType
            NonDeletedRecords = 0
            DeletedRecords = 1
            AllRecords = 2
        End Enum

        Protected MustOverride Function CreateCollectionInstance() As EntityCollection

        Protected MustOverride Sub SetClassData(ByRef entity As Entity, ByVal rsAccount As Recordset)

        Protected MustOverride Function StoredProcSelect() As String

        Protected MustOverride Function CreateClassInstance() As Entity


        ' Returns a filled Collection with the rows specified by Primary Key
        ' parameter(s). If DeletedRecords = True then it will only return items in the
        ' list marked for deletion.
        Public Function GetECCClass(ByVal SecurityToken As String, Optional ByVal ID As Integer = IS_MISSING_OPTIONAL_PARAM_INT, Optional ByVal DeletedRecords As DeleteRecordType = DeleteRecordType.NonDeletedRecords) As EntityCollection

            Dim ErrorNum As Integer
            Dim oDALEng As IOBPDA.IOBPConnection
            Dim rs As ADOR.Recordset
            Dim oCol As EntityCollection
            Dim oClass As Entity
            Dim vParameters(0, 0) As Object
            Dim col As ArrayList
            Dim inx As Integer
            GetECCClass = Nothing

            'Check security token first
            If Trim(SecurityToken) = "" Then
                Err.Raise(ERR_ACCOUNTINVALIDSECURITYTOKEN, "IOBPAccountEng.GetAccount PROC", "Invalid security token.  Security Token can not be empty.")
            End If

            ReDim vParameters(PARMUBOUND, 0)

            If IsMissing(ID) Or Trim(CStr(ID)) = "" Then
                vParameters(PARMVALUE, 0) = System.DBNull.Value
            Else
                vParameters(PARMVALUE, 0) = ID.ToString 'Value
            End If

            vParameters(PARMNAME, 0) = PARMNAMESP_ACCOUNTACCOUNTID 'Name
            vParameters(PARMTYPE, 0) = PARMTYPESP_ACCOUNTACCOUNTID 'Type
            vParameters(PARMLENGTH, 0) = 0 'Size
            vParameters(PARMDIR, 0) = IOBPDA.Direction.adInput 'Direction

            If DeletedRecords <> DeleteRecordType.AllRecords Then
                If Not IsEmptyArray(vParameters) Then
                    inx = UBound(vParameters, 2) + 1
                    ReDim Preserve vParameters(PARMUBOUND, inx)
                Else
                    inx = 0
                    ReDim vParameters(PARMUBOUND, inx)
                End If

                vParameters(PARMNAME, inx) = PARMNAMESP_ACCOUNTDELETEFLAG 'Name        
                vParameters(PARMTYPE, inx) = PARMTYPESP_ACCOUNTDELETEFLAG 'Type
                vParameters(PARMLENGTH, inx) = 0 'Size
                vParameters(PARMDIR, inx) = IOBPDA.Direction.adInput 'Direction
                vParameters(PARMVALUE, inx) = DeletedRecords 'Value
            End If

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

            If Not IsEmptyArray(vParameters) Then
                If Not oDALEng.Execute(SecurityToken, StoredProcSelect, vParameters, rs) Then
                    Err.Raise(ERR_ACCOUNTDALCALLFAILED, "IOBPAccountEng.Get PROC", "Call to Database Failed. SPName was: " & StoredProcSelect())
                End If
            Else
                If Not oDALEng.Execute(SecurityToken, StoredProcSelect, , rs) Then
                    Err.Raise(ERR_ACCOUNTDALCALLFAILED, "IOBPAccountEng.Get PROC", "Call to Database Failed. SPName was: " & StoredProcSelect())
                End If
            End If

            oCol = CreateCollectionInstance()

            If (Not rs Is Nothing) Then 'Set to True if nothing returned in DB
                'Update collection with returned data
                col = New ArrayList()
                Do Until rs.EOF
                    oClass = CreateClassInstance()
                    With oClass
                        .ClassStorage = True 'Set this to False to Enable Data Validation

                        SetClassData(oClass, rs)

                        .SecurityToken = SecurityToken
                        .IsNew = False
                        .Dirty = False
                        .ClassStorage = False
                    End With
                    col.Add(oClass) 'OPTIONAL: You could set the Get string on the Collection
                    rs.MoveNext()
                Loop
            End If

            If Not oCol.Load(SecurityToken, col) Then
                Err.Raise(ERR_ACCOUNTLOADFAILED, "IOBPAccountEng.GetAccount PROC", "Load failed for private collection")
            End If
            'Success
            GetECCClass = oCol
            Erase vParameters
        End Function


        ' Returns an empty Collection so that new Classes can be added.
        Public Function NewCol(ByVal SecurityToken As String) As EntityCollection

            Dim oECCCol As EntityCollection
            'Check security token first
            If Trim(SecurityToken) = "" Then
                Err.Raise(ERR_ACCOUNTINVALIDSECURITYTOKEN, "IOBPAccountEng.NewAccount PROC", "Invalid security token.  Security Token can not be empty.")
            End If

            oECCCol = CreateCollectionInstance()
            oECCCol.SecurityToken = SecurityToken
            NewCol = oECCCol

        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