【问题标题】:condense c# dnn module settings code压缩 c# dnn 模块设置代码
【发布时间】:2012-02-05 09:38:52
【问题描述】:

这是我在大多数 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"]));
}

想象一下像这样加载几个模块的复杂性。啊。


更新

感谢 Olivier,我编写了一个新类来管理属性

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、TreeModuleID、PersonTabID 和 PersonModuleID 也将可用。这就是我想要的,它使用更少的代码,所以感谢 Olivier!

放弃 try catch 会很好,但不能保证该值会在下拉列表中,否则它会更小。不过还是更好。

【问题讨论】:

  • 我曾经读过(我认为是在 Coding Horror 上)从不放置描述 what 代码所做的源代码注释,而只放置描述 的 cmets为什么代码是这样写的。
  • 我相信。我刚刚在某处读到,如果你必须评论你的所作所为,那就意味着前方有麻烦。这让我觉得我需要清理它,因为这有点尴尬。 :-\
  • @Lloyd 自注释代码通常比具有适当 cmets 的普通代码可读性差。

标签: c# properties settings dotnetnuke


【解决方案1】:

为您的设置创建一个包装类。

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

【讨论】:

  • 酷!您能否再解释一下设置集合。我不明白那部分。
  • 您正在访问变量TabModuleSettings。它必须是某种集合类型。弄清楚它是哪种类型(只需将鼠标悬停在它上面并等待工具提示)。然后将我示例中的“SettingsCollection”替换为真实类型。
  • 太棒了!是的,我明白了。这真的很优雅。那么这些可以在整个命名空间中使用,对吧?
  • 完美!谢谢你。如果可以的话,我会投票给你,但我还没有信誉。
  • 如果您希望能够从不同的地方访问您的包装器,而不必每次都创建一个新实例,您可以在适当的类中创建一个静态属性。我经常有一个名为“Config”的静态类来处理这些事情。 Config.Settings.TreeTabID
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
相关资源
最近更新 更多