Search code examples
asp.netvb.netoopencapsulation

Way to encapsulate methods from default.aspx.vb page to another class


This question is about structure :

I have a Default.aspx page which holds references to (XML)services, and handles innerHTML of HTML objects. The ammount of buttons is based on services output.

Since this is an long and complex algorithm, I would like to encapsulate this in another class, to divide it into smaller and more readable chunks of code.

The problem is I do not know what the best option is, should I copy the reference of the used objects(service as well as the HTML items) into the new class ?

Since the ammount and origin of items it does not look to my like an elegant option. I searched on the internet but could not find anything that suits this(I would think) common situation

This is the function I would like to transfer to another class. Currently it is in Default.aspx and uses rep(ort)Service,defaultPath,path,selectionScreen and Image2 objects to draw the menu dynamically.

''' <summary>
''' Dynamically builds the square menu where the reports can be selected from.
''' It is based on the number of reports available
''' Show list of available reports on reporting server as HyperLinks
''' </summary>
''' <remarks></remarks>
Private Sub BuildReportSelectionArea(Optional path As String = "")

    Dim items As CatalogItem() = repService.ListChildren(path, False)
    Dim items2 As CatalogItem() = repService.ListChildren(path, False)

    'Ensure that folders are shown first
    Dim maxFolder = 0
    For i = 0 To items.Count - 1 Step 1
        If (items(i)).TypeName = "Folder" Then
            items(i) = items2(maxFolder)
            items(maxFolder) = items2(i)
            maxFolder += 1
        End If
' Some other code
End Sub

        'TODO :Ensure the alfabetical order is preserved
    Next

Solution

  • I would first generally comment on the code:

    this means you are 2 times accessing the service, but the second array is later used to "sort" the items catalogItem, it seems a waste of resources to call the service twice

    Dim items As CatalogItem() = repService.ListChildren(path, False)
    Dim items2 As CatalogItem() = repService.ListChildren(path, False)
    

    Reordering you could simply achieve using

        Dim items As New List(Of CatalogItem)(RepService.ListChildren(path, False))
        items.Sort(Function(item1 As CatalogItem, item2 As CatalogItem)
                       If String.Equals(item1.TypeName, item2.TypeName) Then
                           Return item1.FileName.CompareTo(item2.FileName)
                       End If
                       If item1.TypeName = "Folder" Then
                           Return -1
                       End If
                       Return 1
                   End Function)
    

    which would sort by folders first, then by filename (you might have to update some of your properties to match)

    you could further extract by either creating a module or a shared class that accepts the repService as an attribute and the path, and returns your output code

    though creating a user control / webpart so you could add this functionality to each page you would like, would be a very good option as well, and the generally accepted way to refactor complex code...