繁体   English   中英

将方法从default.aspx.vb页封装到另一个类的方法

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

这个问题是关于结构的:

我有一个Default.aspx页,其中包含对(XML)服务的引用,并处理HTML对象的innerHTML。 按钮的数量基于服务输出。

由于这是一个漫长而复杂的算法,因此我想将其封装在另一个类中,以将其划分为更小和更易读的代码块。

问题是我不知道最好的选择是什么,我应该将使用过的对象(服务以及HTML项)的引用复制到新类中吗?

由于物品的数量和来源,在我看来它不是一个不错的选择。 我在互联网上搜索,但找不到适合这种(我认为)常见情况的内容

这是我想转移到另一个类的功能。 当前它在Default.aspx中,并使用rep(ort)Service,defaultPath,path,selectionScreen和Image2对象动态绘制菜单。

''' <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

首先,我将首先对代码进行评论:

这意味着您要两次访问该服务,但是第二个数组稍后用于对商品catalogItem进行“排序”,两次调用该服务似乎浪费资源

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

重新排序可以简单地实现

    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)

它将首先按文件夹排序,然后按文件名排序(您可能必须更新某些属性以进行匹配)

您可以通过创建将repService作为属性和路径并返回输出代码的模块或共享类来进一步提取数据

尽管创建了用户控件/ Webpart,因此您可以将此功能添加到所需的每个页面上,这也是一个很好的选择,也是重构复杂代码的公认方法...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM