これは私の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!
をキャッチしようとするといいでしょう。をキャッチしようとしますが、値がドロップダウンリストにあるという保証はありません。それ以外の場合はさらに小さくなります。まだ良いですが。
この方法でコードが記述されたコメント –
私はそれを信じています。私はちょうどあなたがあなたのやり遂げていることが先のトラブルを意味するとコメントしなければならない場合、どこかを読んでいますそれは恥ずかしいので、私はそれをきれいにする必要があると思うに私を持っているものです。 : - \ – Lloyd
@Lloyd通常、自己コメントのコードは普通のコードより読みにくく、適切なコメントが付きます。 –