设计设置结构

时间:2010-12-06 12:14:53

标签: c#

我有一个问题,我想为我的应用设计一个设置结构,在本地化,扩展和分组方面需要尽可能最佳。我想按实体类型对设置进行分组(您可以将其视为每个控制器的分组设置)。设置将显示给用户,因此每个设置都需要一个漂亮的标题和描述,这两者都需要本地化。新设置只会由开发人员引入,并且需要重新编译。

我想到的是一个将设置公开为静态属性的类,因此可以方便地以静态方式在整个应用程序中使用它们。第一次构造类时会加载设置(在请求设置时会发生这种情况),我使用数据库存储设置并使用反射在运行时将它们分配给相应的属性。

看起来像这样

public class FirmSettings
{
    private static IFirmSettingsRepository _repository { get; set; }

    public static bool ShowInvoicePaymentDetails { get; set; }

    public static bool ShowInvoiceDiscountValue { get; set; }

    public static bool ShowDocumentComment { get; set; }

    public static bool ShowDocumentTaxStatement { get; set; }

    public static bool ShowDocumentAuthor { get; set; }

    #region Constructors

    /// <summary>
    ///     Initializes a new instance of the <see cref = "FirmSettings" /> class.
    /// </summary>
    static FirmSettings()
    {
        Load();
    }

    #endregion

    #region Load Settings

    public static void Load()
    {
        _repository = MvcApplication.Container.Get<IFirmSettingsRepository>();
        Type settingsType = typeof (FirmSettings);

        //------------------------------------------------------------
        //  Enumerate through individual settings nodes
        //------------------------------------------------------------
        StringDictionary dic = _repository.LoadSettings();

        if (dic == null)
        {
            Save(); // prepares the settings with blank settings
            dic = _repository.LoadSettings(); // reload
        }

        foreach (string key in dic.Keys)
        {
            //------------------------------------------------------------
            //  Extract the setting's name/value pair
            //------------------------------------------------------------
            string name = key;
            string value = dic[key];

            //------------------------------------------------------------
            //  Enumerate through public properties of this instance
            //------------------------------------------------------------
            foreach (PropertyInfo propertyInformation in settingsType.GetProperties(BindingFlags.Public |
                                                                                    BindingFlags.Static))
            {
                //------------------------------------------------------------
                //  Determine if configured setting matches current setting based on name
                //------------------------------------------------------------
                if (propertyInformation.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                {
                    //------------------------------------------------------------
                    //  Attempt to apply configured setting
                    //------------------------------------------------------------
                    try
                    {
                        if (propertyInformation.CanWrite)
                        {
                            propertyInformation.SetValue(typeof (FirmSettings),
                                                         Convert.ChangeType(value, propertyInformation.PropertyType,
                                                                            CultureInfo.CurrentCulture), null);
                        }
                    }
                    catch
                    {
                        // TODO: Log exception to a common logging framework?
                    }
                    break;
                }
            }
        }

        // perform resave if there are any new settings
        Save();
    }

    #endregion

    #region Save settings

    /// <summary>
    ///     Saves the settings to disk.
    /// </summary>
    public static void Save()
    {
        StringDictionary dic = new StringDictionary();
        Type settingsType = typeof (FirmSettings);

        //------------------------------------------------------------
        //  Enumerate through settings properties
        //------------------------------------------------------------
        foreach (PropertyInfo propertyInformation in settingsType.GetProperties(BindingFlags.Public |
                                                                                BindingFlags.Static))
        {
            //------------------------------------------------------------
            //  Extract property value and its string representation
            //------------------------------------------------------------
            object propertyValue = propertyInformation.GetValue(typeof (FirmSettings), null);

            string valueAsString;
            //------------------------------------------------------------
            //  Format null/default property values as empty strings
            //------------------------------------------------------------
            if (propertyValue == null || propertyValue.Equals(Int32.MinValue) || propertyValue.Equals(Single.MinValue))
            {
                valueAsString = String.Empty;
            }
            else
            {
                valueAsString = propertyValue.ToString();
            }
            //------------------------------------------------------------
            //  Write property name/value pair
            //------------------------------------------------------------
            dic.Add(propertyInformation.Name, valueAsString);
        }

        _repository.SaveSettings(dic);
    }

    #endregion
}

每个设置都存储在DB中作为属性名称的小写版本(对于加载我们忽略大小写)。本地化字符串也是如此,它将存储为例如 FirmSettings_ShowDocumentTaxStatement_Title FirmSettings_ShowDocumentTaxStatement_Desc 。 (约定)

然而,这种方法并不能解决分组问题。在UI中,将需要某种设置分组,因此发票设置将显示在组中。我可以为某些设置引入前缀,然后根据前缀(另一种约定)将其渲染出来。

你喜欢这种方法吗?如果没有,你是怎么做到的?这种方法有很多惯例,这就是困扰我的东西,只是一点点。

2 个答案:

答案 0 :(得分:1)

你在这里迷了我......

我看到你使用某种类型的容器,那么为什么不在每次需要引用时只注入该设置类的单例实例?静态类+方法不适合单元测试(您需要这样做)。

此外,我不明白为什么你想使用Reflections /字符串匹配来设置存储/检索。如果你真的有大量的设置与它们之间的复杂分组,你需要花时间来提出适当的DAL。

只需注意,您的“密钥”(例如,FirmSettings_ShowDocumentTaxStatement_Title)不包含名称空间,因此如果两个类具有相同的名称和相同的方法,您将最终得到一个难以捕获的错误。这只是一个简单的场景。我的观点是,为了识别目的,匹配您的类+方法名称的字符串不是一个好主意。 (因为我假设你有一个很复杂的项目来采用这种设置管理。)

最后,“我不知道除了使用反射之外,你还会在运行时如何为静态(或常规)属性赋值。”您可以使用类/方法/属性属性,并使用工厂类泵出(在您的情况下)所需设置类的单例。相应的DB列/行关联信息可以包含在属性中。

P.S。在性能方面反思很好。只是不要使用静态类,而是使用单例,并在初始化设置类时执行后台任务。启动单例后,您不必再次初始化它。但无论你做什么,我强烈建议你丢失与类/方法名称匹配的字符串。

P.P.S。查看AoP /策略注入(或者它是否干预Microsoft Unity DI容器)。我相信这些对你有帮助吗?

P.P.P.S。最后进入3个帖子的剧本是糟糕的英语...

答案 1 :(得分:0)

恕我直言,解决方案太复杂了。试着想出更简单的东西。首先不要使用反射(性能原因),然后我会使用配置文件(xml)而不是应用程序/用户设置的数据库。

请参阅this有关反思和效果的综合文章。