2014-10-06 56 views
0

這個問題是關於結構:方式來封裝從default.aspx.vb頁面方法到另一個類

我有持有引用(XML)服務Default.aspx頁面,並處理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 
+0

我想這完全取決於你的需求?你能顯示一些代碼嗎?看起來你的服務做得比他們應該做的要多,我把外觀和感覺看作是一個樣式選項,你可以簡單地提取到CSS,並更新XML服務,而不是更改生成的按鈕的類?你能抽象一下你的代碼嗎? – Icepickle 2014-10-06 10:34:19

+0

我粘貼上面的代碼,感謝您的幫助 – 2014-10-06 10:55:56

+0

對不起,實際上我不是tchem的樣式,但添加到HTML中,我將第一個註釋更改 – 2014-10-06 11:02:42

回答

1

我首先一般的代碼註釋:

這意味着你有2次訪問該服務,但第二排以後用於「排序」的項目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,所以你可以添加這個功能到你想要的每個頁面,這也是一個非常好的選擇,以及重構複雜代碼的普遍接受的方式。 ..

+0

謝謝,我正在尋找這種分揀解決方案,但無法在此特定項目組上進行分揀工作! 在這種情況下,只有1個網頁,所以不需要webpart,非常感謝。我將使用模塊或共享類 – 2014-10-06 12:06:38