2012-01-08 71 views
2

這是我在大多數DNN設置模塊中使用的代碼塊,但它看起來過於冗長。你會如何壓縮這個以使其更加可用或至少減少多餘?condense c#dnn模塊設置代碼

/// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// LoadSettings loads the settings from the Database and displays them 
    /// </summary> 
    /// ----------------------------------------------------------------------------- 
    public override void LoadSettings() 
    { 
     try 
     { 
      if (Page.IsPostBack == false) 
      { 
       ddlTreeTabId.DataSource = GetTabs(); 
       ddlTreeTabId.DataBind(); 
       if (!string.IsNullOrEmpty((string)TabModuleSettings["TreeTabID"])) 
       { //Look for the tree tab id 
        this.ddlTreeTabId.SelectedValue = (string)TabModuleSettings["TreeTabID"]; 
        //If we're here, we have a tab module id, now we can grab the modules on that page 
        LoadTabModules(ddlTreeModuleID, int.Parse((string)TabModuleSettings["TreeTabID"])); 

        //we only do this part if the proceeding steps checked out 
        //if we have a tree module id 
        if (!string.IsNullOrEmpty((string)TabModuleSettings["TreeModuleID"])) 
        { 
         try 
         { 
          //carefully try to select that item from the module id drop down list 
          this.ddlTreeModuleID.SelectedValue = (string)TabModuleSettings["TreeModuleID"]; 
         } 
         catch (Exception ex) 
         { //There may have been a module id but it aint on that page any more. Ignore the error. 
          // I hate invoking try just to ignore an error. seems wasteful. 
         } 
        } 
       } 
     } 
     catch (Exception exc) //Module failed to load 
     { 
      Exceptions.ProcessModuleLoadException(this, exc); 
     } 
    } 

我的理想的解決方案會以這樣的方式,在整個模塊可以在不輸入這一切將返回樹模塊ID實現的屬性。

if (!string.IsNullOrEmpty((string)Settings["TreeTabID"]) && 
    !string.IsNullOrEmpty((string)Settings["TreeModuleID"])) 
{ 
    Do_SomethingWithTheIDs(
     int.Parse((string)Settings["TreeTabID"]), 
     int.Parse((string)Settings["TreeModuleID"])); 
} 

想象一下像加載這樣幾個模塊的複雜性。啊。


UPDATE

感謝奧利弗,我已經寫了一個新的類來管理屬性

public class TreeSettingsBase : ModuleSettingsBase 
{ 
    public int? TreeTabID 
    { 
     get 
     { 
      string s = (string)Settings["TreeTabID"]; 
      int id; 
      return Int32.TryParse(s, out id) ? id : (int?)null; 
     } 
    } 

    public int? TreeModuleID 
    { 
     get 
     { 
      string s = (string)Settings["TreeModuleID"]; 
      int id; 
      return Int32.TryParse(s, out id) ? id : (int?)null; 
     } 
    } 

    public int? PersonTabID 
    { 
     get 
     { 
      string s = (string)Settings["PersonTabID"]; 
      int id; 
      return Int32.TryParse(s, out id) ? id : (int?)null; 
     } 
    } 

    public int? PersonModuleID 
    { 
     get 
     { 
      string s = (string)Settings["PersonModuleID"]; 
      int id; 
      return Int32.TryParse(s, out id) ? id : (int?)null; 
     } 
    } 
} 

所以現在我LoadSettings看起來是這樣的:

public override void LoadSettings() 
    { 
     try 
     { 
      if (Page.IsPostBack == false) 
      { 
       ddlTreeTabId.DataSource = GetTabs(); 
       ddlTreeTabId.DataBind();    
       if (TreeTabID.HasValue) 
       { 
        this.ddlTreeTabId.SelectedValue = TreeTabID.ToString(); 
        LoadTabModules(ddlTreeModuleID, TreeTabID.Value); 
        if (TreeModuleID.HasValue) 
        { 
         try 
         { 
          this.ddlTreeModuleID.SelectedValue = TreeModuleID.ToString(); 
         } 
         catch (Exception ex) 
         { 
         } 
        } 
       } 
       ddlPersonTabId.DataSource = GetTabs(); 
       ddlPersonTabId.DataBind(); 
       if (PersonTabID.HasValue) 
       { 
        this.ddlPersonTabId.SelectedValue = PersonTabID.ToString(); 
        LoadTabModules(ddlPersonModuleID, PersonTabID.Value); 
        if (PersonModuleID.HasValue) 
        { 
         try 
         { 
          this.ddlPersonModuleID.SelectedValue = PersonModuleID.ToString(); 
         } 
         catch (Exception ex) 
         { 
         } 
        } 
       } 

      } 
     } 
     catch (Exception exc) //Module failed to load 
     { 
      Exceptions.ProcessModuleLoadException(this, exc); 
     } 
    } 

ModuleSettingsBase隨處可用,因此TreeTabID,Tree ModuleID,PersonTabID和PersonModuleID也是如此。這就是我想要的,它使用更少的代碼,所以謝謝Olivier!

這將是很好的溝try catch但不能保證價值將在下拉列表中,否則它會更小。但仍然更好。

+0

我曾經看過(我認爲這是對編碼恐怖),以從來沒有把源代碼註釋描述_what_代碼的功能,但只放描述_why_代碼的評論是這樣寫的。 – 2012-01-08 14:58:37

+1

我相信。我剛剛在某處讀到,如果你必須評論你的做法,這意味着未來的麻煩。這讓我想到我需要清理它,因爲它有點尷尬。 : - \ – Lloyd 2012-01-08 15:00:49

+0

@Lloyd自評論代碼通常比具有正確評論的普通代碼更不可讀。 – 2012-01-08 15:20:33

回答

2

爲您的設置創建包裝類。

public class TabModuleSettingsWrapper { 

    private SettingsCollection _settings; // I do not know of which type your settings are. 

    public TabModuleSettingsWrapper(SettingsCollection settings) { 
     _settings = settings; 
    } 

    public int? TreeModuleID { 
     get { 
      string s = (string)_settings["TreeModuleID"]; 
      int id; 
      return Int32.TryParse(s, out id) ? id : (int?)null; 
     } 
    } 

    // Repeat this for all the settings 
} 

現在你可以訪問設置:

var settings = new TabModuleSettingsWrapper(TabModuleSettings); 
if (settings.TreeTabID.HasValue && settings.TreeModuleID.HasValue) { 
    Do_SomethingWithTheIDs(settings.TreeTabID, settings.TreeModuleID); 
} 
+0

酷!你能否更詳細地解釋設置集合?我沒有那部分。 – Lloyd 2012-01-08 15:14:21

+0

您正在訪問變量'TabModuleSettings'。它必須是某種集合類型。找出它的類型(只需將鼠標懸停在上面並等待工具提示即可)。然後將我的示例中的「SettingsCollection」替換爲實際類型。 – 2012-01-08 15:36:06

+0

太棒了!是的,我明白了。這真的很高雅。這些可以在整個命名空間中使用,對吧? – Lloyd 2012-01-08 16:34:42