最近,我发现“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 之类的东西就会被完全清空,从而使我的 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-Key,但使用 Auto-Incr Index 代替。这样,如果我有一个应用程序需要将一些东西邮寄给多个经理,我可以存储很多:
index component setting value
1 RequestModule ManagerEmail manager1@someplace
2 RequestModule ManagerEmail manager2@someplace
然后我可以使用:
IEnumerable<string> GetValues(string componentName, string settingName);
它将返回一个电子邮件地址列表,而不仅仅是一个值。
这有意义吗?