这是我在大多数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但不能保证价值将在下拉列表中,否则它会更小。但仍然更好。
我曾经看过(我认为这是对编码恐怖),以从来没有把源代码注释描述_what_代码的功能,但只放描述_why_代码的评论是这样写的。 – 2012-01-08 14:58:37
我相信。我刚刚在某处读到,如果你必须评论你的做法,这意味着未来的麻烦。这让我想到我需要清理它,因为它有点尴尬。 : - \ – Lloyd 2012-01-08 15:00:49
@Lloyd自评论代码通常比具有正确评论的普通代码更不可读。 – 2012-01-08 15:20:33