【发布时间】:2010-12-06 12:14:52
【问题描述】:
我有一个问题,我想为我的应用设计一个设置结构,该结构需要在本地化、扩展和分组方面尽可能优化。我想对每个实体类型的设置进行分组(您可以将其视为每个控制器的分组设置)。设置将显示给用户,因此每个设置都需要一个漂亮的标题和描述,这两者都需要本地化。新设置只能由开发人员引入,需要重新编译。
我想出的是一个将设置公开为静态属性的类,因此它们很容易以静态方式在整个应用程序中使用。设置在第一次构造类时加载(在请求设置时发生),我使用数据库来存储设置并在运行时使用反射将它们分配给它们的相应属性。
看起来像这样
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 中,需要某种设置分组,因此发票设置将显示在一个组中。我可以为某些设置引入前缀,然后根据前缀(另一种约定)呈现出来。
你喜欢这种方法吗?如果没有,你怎么做?这种方法有很多约定,这就是困扰我的地方,只是一点点。
【问题讨论】:
标签: c#