最近,我发现"Web.Config"文件包含一个<appSettings>
部分,似乎很适合存储应用程序设置。它甚至有一种通过标准系统库访问该文件的编程方式。所以我聪明地写了一个接口来访问它,然后是一个实现该接口的具体实现,如下所示:
public interface IAppSettings
{
IEnumerable<string> GetValues(string componentName, string settingName);
IEnumerable<KeyValuePair<string, string>> GetValuePairs(string componentName, string settingName);
void SetValues(string componentName, string settingName, IEnumerable<string> valueList, bool append);
void SetValuePairs(string componentName, string settingName, IEnumerable<KeyValuePair<string, string>> pairList, bool append);
}
我发现在应用程序运行时将设置保存回“web.config”会导致整个应用程序重新启动,这对我来说完全不合理,因为如果我经常写回web.config并且每次应用程序重启,那么像HttpRuntime.Cache这样的东西就会被彻底清空,使我的缓存无效,因为它不断地清空和重新填充。
所以我想知道:我应该把我的应用程序设置存储在哪里?
是否有一个好的解决方案,这样我就不必自己开发?
编辑:
好的,感谢所有建议使用数据库和潜在表模式的人。我想我会采用以下模式:
settings:
index NUMBER NOT NULL AUTO_INCREMENT <== Primary Key
component NVARCHAR(255) NOT NULL
setting NVARCHAR(255) NOT NULL
key NVARCHAR(255)
value NVARCHAR(255) NOT NULL
虽然我不认为我会将“设置”为P键,而是使用自动递增索引。这样,如果我有一个需要向多个经理发送邮件的应用程序,我可以存储许多内容。
index component setting value
1 RequestModule ManagerEmail manager1@someplace
2 RequestModule ManagerEmail manager2@someplace
然后我可以使用:
IEnumerable<string> GetValues(string componentName, string settingName);
它将返回一个电子邮件地址列表,而不仅仅是单个值。
这样说清楚了吗?