2012-01-08 6 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

おかげで、私は、だから今、私LoadSettings性質

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; 
     } 
    } 
} 

を管理するための新しいクラスを書いてきた次のようになります。

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!

をキャッチしようとするといいでしょう。をキャッチしようとしますが、値がドロップダウンリストにあるという保証はありません。それ以外の場合はさらに小さくなります。まだ良いですが。

+0

この方法でコードが記述されたコメント –

+1

私はそれを信じています。私はちょうどあなたがあなたのやり遂げていることが先のトラブルを意味するとコメントしなければならない場合、どこかを読んでいますそれは恥ずかしいので、私はそれをきれいにする必要があると思うに私を持っているものです。 : - \ – Lloyd

+0

@Lloyd通常、自己コメントのコードは普通のコードより読みにくく、適切なコメントが付きます。 –

答えて

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 
} 

今あなたが設定にアクセスすることができます_what_コードが行う記述するソースコードのコメントを入れていないが、唯一入れないために(私はそれがホラーコーディング上だったと思う)私は一度読ん

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

クール!あなたは設定のコレクションをもう少し説明できますか?私はその部分を取得しません。 – Lloyd

+0

変数 'TabModuleSettings'にアクセスしています。いくつかのコレクションタイプでなければなりません。どのタイプからのものなのかを把握してください(マウスの上にマウスを置いて、ツールチップを待つだけです)。次に、私の例の "SettingsCollection"を実際の型に置き換えます。 –

+0

恐ろしい!うん、私はそれを得る。それは本当にエレガントです。これらは、ネームスペース全体で使用可能になる可能性があります。 – Lloyd

関連する問題