其实我已经实现了这个在C#中的键/值对与NHibernate。该表与您仅用一个键/值配对所描述的相同。当我想获取/设置一个值时,我的持久层接受“键”(作为一个枚举)并返回一个ApplicationSetting对象。这个对象实际上只是键/值对的包装。
简单的例子(C#):
class ApplicationSetting
{
private string theApplicationSettingString = string.Empty;
/// <summary>
/// The name of the setting.
/// </summary>
public virtual ApplicationSettingKey SettingName
{
//I use enumerations here so that I'm guaranteed to get a key I
//already know about and not a random string.
get
{
return (ApplicationSettingKey)Enum.Parse(
typeof(ApplicationSettingKey), theApplicationSettingString);
}
set
{
theApplicationSettingString =
Enum.GetName(typeof(ApplicationSettingKey), value);
}
}
/// <summary>
/// The value of the setting.
/// </summary>
public virtual string SettingValue
{
get;
set;
}
}
/// <summary>
/// Enumeration for all application settings.
/// </summary>
public enum ApplicationSettingKey
{
WIDTH,
HEIGHT,
SHOWALL
}
/// <summary>
/// Returns the ApplicationSetting from the database that corresponds to
/// the passed in key/name.
/// </summary>
/// <param name="aKey">The key/name of the Application setting to
/// retrieve.</param>
/// <returns>The ApplicationSetting Definition that with the corresponding
/// application setting key/name.</returns>
public ApplicationSetting GetApplicationSettingByKey(ApplicationSettingKey aKey)
{
const string propertyName = "theApplicationSettingString";
string key = Enum.GetName(typeof(ApplicationSettingKey), aKey);
DetachedCriteria criteria = DetachedCriteria.For<ApplicationSetting>();
criteria.Add(Restrictions.Eq(propertyName, key));
return FindFirst(criteria);
}
是否真的需要将这些存储在SQL数据库中?你是否会加入这些数据与其他数据,从其他应用程序查询它等?或者它只是存储它最方便的地方? – 2010-06-23 19:51:17
在大多数情况下,我会说不。但在这种情况下,我正在处理两个不同的应用程序(在不同的机器上),这两个应用程序都在同一个数据库上工作。 – 2010-06-23 20:45:39