我想到的是一个将设置公开为静态属性的类,因此可以方便地以静态方式在整个应用程序中使用它们.第一次构造类时会加载设置(在请求设置时会发生这种情况),我使用数据库存储设置并使用反射在运行时将它们分配给相应的属性.
看起来像这样
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 property@R_268_4045@ion in settingsType.GetProperties(BindingFlags.Public | BindingFlags.Static)) { //------------------------------------------------------------ // Determine if configured setting matches current setting based on name //------------------------------------------------------------ if (property@R_268_404[email protected](name,StringComparison.OrdinalIgnoreCase)) { //------------------------------------------------------------ // Attempt to apply configured setting //------------------------------------------------------------ try { if (property@R_268_404[email protected]) { property@R_268_404[email protected](typeof (FirmSettings),Convert.ChangeType(value,property@R_268_404[email protected],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 property@R_268_4045@ion in settingsType.GetProperties(BindingFlags.Public | BindingFlags.Static)) { //------------------------------------------------------------ // Extract property value and its string representation //------------------------------------------------------------ object propertyValue = property@R_268_404[email protected](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(property@R_268_404[email protected],valueAsstring); } _repository.SaveSettings(dic); } #endregion }
每个设置都存储在DB中作为属性名称的小写版本(对于加载我们忽略大小写).本地化字符串也是如此,它将被存储为例如FirmSettings_ShowDocumentTaxStatement_Title和FirmSettings_ShowDocumentTaxStatement_Desc. (惯例)
然而,这种方法不能解决分组问题.在UI中,将需要某种设置分组,因此发票设置将显示在组中.我可以为某些设置引入前缀,然后根据前缀(另一种约定)将其渲染出来.
解决方法
我看到你使用某种类型的Container,那么为什么不在每次需要引用时只注入该设置类的单例实例?静态类方法不适合单元测试(您需要这样做).
此外,我不明白为什么你想使用Reflections /字符串匹配来设置存储/检索.如果你真的有大量的设置与它们之间复杂的分组,你需要花时间来提出一个合适的DAL.
只需注意,您的“密钥”(例如,FirmSettings_ShowDocumentTaxStatement_Title)不包含名称空间,因此如果两个类具有相同的名称和相同的方法,您将最终得到一个难以捕获的错误.这只是一个简单的场景.我的观点是,为了识别目的,匹配您的类方法名称的字符串不是一个好主意. (因为我假设你有一个很复杂的项目来采用这种设置管理.)
最后,“我不知道除了使用反射之外,你还会在运行时如何为静态(或常规)属性赋值.”您可以使用类/方法/属性属性,并使用工厂类泵出(在您的情况下)所需设置类的单例.适当的DB列/行关联信息可以包含在属性中.
附:在性能方面反思很好.只是不要使用静态类,而是使用单例,并在初始化设置类时执行后台任务.启动单例后,您不必再次初始化它.但无论你做什么,我强烈建议你丢失与类/方法名称匹配的字符串.
P.P.S.查看AoP /策略注入(或者它是否干预Microsoft Unity DI容器).我相信这些对你有帮助吗?
P.P.P.S.最后进入3个帖子的剧本是糟糕的英语…
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。