gpt4 book ai didi

c# - 设计设置结构

转载 作者:行者123 更新时间:2023-11-30 12:52:33 24 4
gpt4 key购买 nike

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

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

看起来像这样

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
}

每个设置都作为属性名称的小写版本存储在数据库中(对于加载,我们忽略大小写)。这同样适用于本地化字符串,例如,将存储为 FirmSettings_ShowDocumentTaxStatement_TitleFirmSettings_ShowDocumentTaxStatement_Desc。 (惯例)

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

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

最佳答案

你在这里失去了我......

我看到您使用了某种容器,那么为什么不在每次需要引用时注入(inject)该设置类的单例实例呢?静态类 + 方法不利于单元测试(为此你需要)。

此外,我不明白为什么要使用反射/字符串匹配来设置存储/检索。如果您确实有大量设置,并且它们之间的分组很复杂,您将需要花时间来想出一个合适的 DAL。

请注意,您的“键”(例如 FirmSettings_ShowDocumentTaxStatement_Title)不包含命名空间,因此如果两个类具有相同的名称和相同的方法,您最终会遇到一个难以发现的错误。这只是一个简单的场景。我的观点是,为了识别目的而匹配您的类 + 方法名称的字符串不是一个好主意。 (因为我假设您有一个复杂的大项目需要使用这种设置管理。)

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

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

附言查看 AoP/策略注入(inject)(或者它是否干预 Microsoft Unity DI 容器)。相信这些也许对你有帮助?

P.P.P.S.最后成3个post 脚本英文不好...

关于c# - 设计设置结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4366276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com