在哪里存储应用程序设置?

发布于 2024-09-28 11:10:52 字数 1772 浏览 1 评论 0原文

最近,我发现“Web.Config”文件包含一个 部分,该部分似乎适合存储应用程序设置。哎呀,它甚至有一种编程方式通过标准系统库访问文件。因此,我很聪明,编写了一个接口来访问它,然后编写了该接口的具体实现,如下所示:

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-Key,但会使用自动增量索引。这样,如果我有一个应用程序需要将某些内容邮寄给多个经理,我可以存储许多:

index     component       setting        value
1         RequestModule   ManagerEmail   manager1@someplace
2         RequestModule   ManagerEmail   manager2@someplace

然后我可以使用:

IEnumerable<string> GetValues(string componentName, string settingName);

它将返回电子邮件地址列表,而不仅仅是一个值。

这有道理吗?

Recently, I discovered that the "Web.Config" file contains an <appSettings> section which seemed good for storing one's Application Settings. Heck, it even has a programmatic way to access the file thru a standard System library. So being all clever, I wrote an Interface to access it and then a Concrete implementation of the interface, shown here:

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);
}

Then I came to discover that saving settings back to "web.config" while the application is running causes the entire application to re-start. This seems completely unreasonable to me because if I'm writing back to web.config a lot and the application is restarting each time, then things like HttpRuntime.Cache get completely emptied effectively making my Cache useless because it's constantly emptying and repopulating.

So I'm wondering: Where should I store my Application Settings?

Is there a good solution out there for this so that I don't have to roll my own?

EDIT:

Okay, thanks to everyone who suggested using a DB and a potential table schema. I think I'm going to go with the following schema:

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

Though I don't think I'll make the "setting" the P-Key, but use an Auto-Incr Index instead. This way if I have an application that needs to mail something to multiple managers, I can store many:

index     component       setting        value
1         RequestModule   ManagerEmail   manager1@someplace
2         RequestModule   ManagerEmail   manager2@someplace

And then I can use:

IEnumerable<string> GetValues(string componentName, string settingName);

And it will return a list of email addresses, rather than just a single value.

Does this make sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

檐上三寸雪 2024-10-05 11:10:52

web.config 一般用于只读设置,即。系统管理员在部署应用程序期间设置的设置。

如果您想读取和写入设置,最明显的方法是使用数据库。顺便说一句,这有一个优点:一个应用程序可以托管在多个服务器上,并且仍然可以正确读取和写入设置,

您还可以为设置实现自定义存储,但实现起来可能会更困难,而且难度不大快点。


要回答第二个问题,数据库的结构取决于您要存储的设置类型。

如果您需要存储异构唯一条目,如下所示:

  • 管理员的邮件地址、
  • 网站主页上显示的最大条目数、
  • “关于我们”页面上显示的文本、
  • 指示的布尔值无论是否启用公共评论,

那么您都必须使用 varchar 或其他或多或少友好的类型作为识别条目的键(而不是通过索引引用它们)。

另一方面,如果您的目的是存储多个经理的邮件地址,则应创建一个 Manager 表,其中包含他们的邮件地址、姓名、上次连接的日期时间等等。

你真的不应该混合两者。理论上,您可以按组件/设置对引用设置中的条目。在实践中,这会让事情变得更加困难并产生一系列问题:

  • 如果您进一步需要为每个经理存储一个布尔值来指示她/他是否希望接收来自您的警报,该怎么办?以您目前的结构,这是不可能的。
  • 由于相同的设置可以有多个值,您打算如何处理必须唯一的设置?例如,“关于我们”页面上只能显示单个文本值。如果数据库中存储了两个值怎么办?

web.config is generally used for read-only settings, ie. the settings set during deployment of the application by the system administrator.

If you want to read and write the settings, the most obvious way is to use the database. By the way, this has an advantage: an application can be hosted on several servers and will still read and write the settings correctly,

You can also implement your custom storage for the settings, but it will be probably more difficult to implement and not much faster.


To answer your second question, the structure of your database depends on the type of settings you want to store.

If you need to store heterogeneous unique entries like this:

  • Mail address of an administrator,
  • Maximum number of entries to display on home page of the website,
  • Text to display on "About us" page,
  • Boolean value indicating whether public comments are enabled or not,

then you have to use varchars or other more or less friendly types as keys to identify the entries (rather than to refer to them by their index).

On the other hand, if your purpose is to store the mail addresses of several managers, you should create a Manager table containing their mail addresses, names, datetime of their last connection, etc.

You really shouldn't mix both. In theory, you can refer to the entry in settings by component/setting pair. In practice, it makes things harder and creates a bunch of problems:

  • What if, further, you will need, for every manager, to store a boolean value indicating whether she/he wants to receive alerts from you? With your current structure, this will be impossible.
  • Since the same setting can have multiple values, how do you intend to handle the settings which must be unique? For example, there must be only a single value of text to display on "About us" page. What if there are two values stored in database?
煮茶煮酒煮时光 2024-10-05 11:10:52

在 web.config 中存储设置非常有用,因为它可以轻松地在不同环境中拥有不同的设置。但是,正如您所说,如果您可能想要更改实时环境中的设置,那么这是没有用的。

如果您需要更改值,简单的数据库表是最有用的方法。

例如。

create table Settings
(Name varchar(50) primary key,
Value varchar(50))

如果您使用的是 SQL Server,则可以将 Value 列设置为 sql_variant,这将允许您存储各种数据类型。

Storing settings in web.config is useful because it makes it easy to have different settings in different environments. However, as you say, this is no use if you are likely to want to change the settings in a live environment.

A simple database table is the most useful way to do this if you need to change the values.

eg.

create table Settings
(Name varchar(50) primary key,
Value varchar(50))

If you're using SQL Server, you can set to the Value column to be sql_variant which will allow you to store a variety of datatypes.

就是爱搞怪 2024-10-05 11:10:52

它适用于应用程序设置,但不适用于在运行时动态更改的设置。相反,它适用于仅偶尔更改的设置,以及您期望(甚至希望)应用程序在更改时重新启动的情况。

对于更短暂的设置,您可能只想使用一个简单的数据库系统 - 如果您的应用程序不使用数据库,即使是 App_Data 目录中的平面文件/XML 也可以工作。

It is meant for application settings, but not settings that are meant to be dynamically changed during runtime. Rather, it is for settings that change only occasionally, and where you would expect (and even desire) an app restart when they change.

For more ephemeral settings, you may want to just use a simple database system - even a flat file/XML in the App_Data dir can work, if your app doesn't use a database otherwise.

猫瑾少女 2024-10-05 11:10:52

是的。更改 web.config 将重置应用程序。我通常维护一个设置表来存储设置的键值对并从那里访问它。

设置

SETTING_NAME   VARCHAR(100)PRIMARY KEY
SETTING_VALUE  VARCHAR(100)

然后编写一个可以向该表插入、删除、更新值的类。

SETTINGS 表的 Ex 数据

    SETTING_NAME         SETTING_VALUE

    AdminEmail            [email protected]
    ErrorTrackingEmail    [email protected]

Yes.Changing the web.config will reset the application.I usually maintain a settings table to store key-value pairs for the settings and access it from there.

SETTINGS

SETTING_NAME   VARCHAR(100)PRIMARY KEY
SETTING_VALUE  VARCHAR(100)

Then write a class which can Insert,Delete,Update values to this table.

Ex Data for the SETTINGS table

    SETTING_NAME         SETTING_VALUE

    AdminEmail            [email protected]
    ErrorTrackingEmail    [email protected]
冰葑 2024-10-05 11:10:52

我通常在设置表中添加一个“类型”字段,以便我只检索所需的设置组,这对我来说是一种对类似设置进行分组并立即检索它们的方式。

I usually add a "type" field to my settings table so that I only retrieve the group of settings needed this works for me as a way of grouping like settings and retrieving them at once.

一直在等你来 2024-10-05 11:10:52

创建一个键值表,如下所示:

settings:
name NVARCHAR(255) PRIMARY KEY
value NVARCHAR(255) NOT NULL

Create a key-valued table like this:

settings:
name NVARCHAR(255) PRIMARY KEY
value NVARCHAR(255) NOT NULL
阳光下的泡沫是彩色的 2024-10-05 11:10:52

首先,当您考虑 web.config 文件中应该存储哪些信息时,这并不是没有道理的。当您更改程序集信息、连接字符串等内容时,您的应用程序需要停止并重新加载值才能使用这些设置运行。

如果您要存储应用程序范围的设置,则可以在数据库中创建设置表,甚至使用单独的文本文件来存储设置。

如果您正在讨论存储每个用户的设置,您应该查看 ASP.NET配置文件属性

First of all, it's not unreasonable at all when you consider what information is supposed to be stored in the web.config file. When you change things like assembly information, connection strings, etc., your application needs to stop and reload the values to run with those settings.

If you're storing application wide settings, then you can create a Settings table in your database or even use a separate text file to store the settings.

If you're talking about storing per-user settings, you shoul check out ASP.NET Profile Properties.

醉梦枕江山 2024-10-05 11:10:52

应用程序范围的设置当然应该存储在 web.config 文件的这一部分中,因为它可以防止可能随时间变化的硬编码值。无需使用您自己的代码读出它们,因为有一个内置方法:使用 System.Configuration.ConfigurationManager.AppSettings 数组来检索它们(您需要添加对项目中的 System.Configuration 程序集)。您也可以通过 ASP.NET 网站管理工具编辑 AppSettings(项目菜单 -> ASP.NET 配置)。

对于您设想在站点启动并运行时更频繁更改的值,可以合理地使用 XML 文件、轻量级数据库(例如 SQLite 或 SQL Server Compact)甚至文本文件来进行这些设置。

Application-wide settings should certainly be stored in this section of the web.config file, as it prevents hard-coding values which may change over time. There's no need to read them out using your own code, as there's a built-in method: use the System.Configuration.ConfigurationManager.AppSettings array to retrieve them (you'll need to add a reference to the System.Configuration assembly in your project). You can edit AppSettings through the ASP.NET Web Site Administration Tool, too (Project menu -> ASP.NET Configuration).

For values which you envisage changing more often and while the site is up and running, it's reasonable to use an XML file, lightweight database (such as SQLite or SQL Server Compact) or even a text file for these settings.

蔚蓝源自深海 2024-10-05 11:10:52

如果需要保存设置,您始终可以将它们保存在自定义配置文件中。

我不久前就这样做了,并且我有可用的代码此处

If you need to save settings, you can always save them in a custom configuration file.

I did just that a while back, and I have the code available to do it here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文