其實我已經實現了這個在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