为什么人们一致推荐使用 appConfig 而不是使用设置文件? (。网)

发布于 2024-07-24 22:22:48 字数 1676 浏览 5 评论 0原文

我经常看到这样的问题的答案:“我应该如何在我的 .NET 应用程序中存储设置?” 是通过手动向 app.config(或 web.config)添加条目来编辑 app.config 文件,如下所示:

<configuration> 
  <appSettings>
    **<add key="ConfigValueName" value="ABC"/>**
  </appSettings>
</configuration>

然后,按如下方式访问它们:

string configValue = Configuration.AppSettings["ConfigValueName"];

我将把上面概述的方法称为“app.config”方法。 我很少看到人们建议向项目添加“设置”文件。 我已经在网络和 stackoverflow 上看到过很多次了……我开始怀疑我是否遗漏了一些东西……因为我不确定为什么你会使用这种方法而不是使用“设置” “ 文件。 我直到 VS2005 才开始接触 .NET,所以我的一个理论是,这就是 VS2003 中的工作方式,人们从未切换过?

人们推荐 app.config 方法的示例:

从我的角度来看,“设置文件”方法有以下优点:

  1. 可用于同一界面中的应用程序设置(所有用户共用的设置)和用户设置。
  2. 能够使用 Visual Studio 中的设置设计器支持。 恕我直言,比直接编辑 XML 文件更不容易出错。
  3. 重构 - 您可以重命名特定的设置名称,它将自动更新代码中的引用。
  4. 编译类型检查。
  5. 自动完成支持。
  6. 属性网格功能。 我发现 PropertyGrid 控件是制作快速选项表单的非常简单的方法。 您只需执行 propertyGrid1.SelectedObject = Settings1.Default; 即可完成。

如果您不确定“设置”文件方法的含义,请参阅此post 这是有人建议使用设置文件而不是 app.confg 的少数示例之一。

编辑:请理解:本主题的目的是弄清楚为什么人们会使用上面概述的 app.config 方法而不是设置文件方法。 我遇到了设置文件方法的局限性,有时被迫推出自己的自定义解决方案。 这是一个完全不同的讨论

Very often I see the answer to the question like: "How should I store settings in my .NET app?" is to edit the app.config file by manually adding entries to the app.config (or web.config) like so:

<configuration> 
  <appSettings>
    **<add key="ConfigValueName" value="ABC"/>**
  </appSettings>
</configuration>

Then, accessing them like:

string configValue = Configuration.AppSettings["ConfigValueName"];

I'll refer to the approach outlined above as the "app.config" approach. Very rarely do I see people recommend adding a "Settings" file to the project. I've seen this so many times across the web and on stackoverflow... I'm starting to wonder if i'm missing something... because I'm not sure why you'd use this method over using a "Settings" file. I didn't get into .NET until VS2005 so one theory I have is this was how things were done in VS2003 and people never switched over?

Examples of people recommending the app.config approach:

From my viewpoint there are following advantages of the "Settings file" approach:

  1. Can be used for both Application Settings (those that are common to all users) and User settings from the same interface.
  2. Ability to use the Settings designer support in visual studio. Less error prone then editing an XML file directly IMHO.
  3. Refactoring - you can rename a particular setting name and it will automatically update references in your code.
  4. Compile type checking.
  5. Auto-completion support.
  6. Property-Grid Capabilities. I have found that the PropertyGrid control is a very easy way to make a quick options form. You just do propertyGrid1.SelectedObject = Settings1.Default; and you're done.

In case you are not sure what I mean by the "Settings" file approach see this post which is one of the few examples where someone recommends using Settings files instead of app.confg.

EDIT: Please understand: The intention of this topic is to figure out why people would use the app.config approach outlined above over the Settings file approach. I have encountered the limitations of the Settings file approach and have been forced to roll my own custom solution at times. That's an entirely different discussion.

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

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

发布评论

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

评论(7

饮湿 2024-07-31 22:22:49

如果没有讨论,那么答案就是:

“因为它更容易。”

Without the discussion, then the answer is:

"Because it's easier."

小镇女孩 2024-07-31 22:22:49

两种情况:

  • 客户端应用程序附带的配置设置设置为默认值。 该应用程序提供了一个 UI 来修改单个用户或所有用户的设置。

在这里,“设置”方法轻而易举地获胜。

  • 一个应用程序由几个相对独立的程序集组成,每个程序集都需要一些简单的配置设置。 这些设置通常由管理员设置,应用程序部署后不会对其进行修改。

这里“appSettings”可以更简单地管理。 通过“设置”方法,每个可配置程序集都会有一个部分添加到主应用程序的配置文件中。

Two scenarios:

  • A client app which is shipped with configuration settings set to default values. The app provides a UI to modify the settings for a single user or all users.

Here the "settings" approach wins hands down.

  • An app consisting of several relatively independent assemblies each of which requires a few simple configuration settings. The settings are normally set by an administrator and are not modified by the application once deployed.

Here "appSettings" can be much simpler to manage. With the "settings" approach, there will be a section for every configurable assembly to be added to the main application's configuration file.

画尸师 2024-07-31 22:22:49

因为设置文件基础结构 (1) 更复杂,(2) 鉴于其复杂性,记录不充分。

设置文件的默认工作方式对于小型应用程序很有用。 对于较大的,您经常需要更改此默认值。 设置文件基础设施可以满足您的需求,但即使有良好的文档,也有一个陡峭的学习曲线。 如果没有文档,它几乎毫无用处。

我只是参考这个文章结束。 请随意粘贴一些有用的链接到概念性 MSDN 文档来反驳我的论点。


更新:

我需要公平地提供一个我没有找到记录的特定用例:

我喜欢应用程序设置设计器。 我还喜欢存储设置的 XML 格式。 我想保留这些功能,但使用其他位置来存储设置。 我没有找到任何提示是否支持此用例,如果支持,如何完成任务。

Because the Settings file infrastructure is (1) more complex and (2) insufficiently documented given its complexity.

The way Settings files work by default are useful for small applications. For bigger ones you often need to change this default. The Settings file infrastructure could be leveraged to your needs, but there is a steep learning curve involved, even with good documentation. Without documentation, it's near to useless.

I just refer to this article to conclude. Feel free to paste some useful links to conceptual MSDN documentation to contradict my argument.


Update:

I need to be fair and supply a specific use case which I didn't find documented:

I like the application settings designer. I also like the XML format of the stored settings. I wanted to retain these features, but use other locations where to store the settings. I didn't find any hint whether this use case was supported, and if yes, how to accomplish the task.

木緿 2024-07-31 22:22:49

不正确:设置文件方法的部分问题在于它要求应用程序位于特定位置并且设置文件位于正确位置。 如果其中之一被意外移动,则设置可能会永久丢失,就像被删除一样。 还可能存在文件夹中存在多个同名设置文件的问题,导致其中一个设置文件被覆盖。 如果软件依赖于设置文件,这尤其糟糕。 这些问题对于已安装的软件来说并不那么明显,但对于未安装但只是下载并运行的软件来说可能是一个主要问题。 想象一下,如果有人将其中几个程序下载到同一目录,并且它们都使用相同的设置文件名。

编辑:正如与提出问题的人讨论的那样,这个答案是不正确的,因为该问题指的是保存到设置文件的两种不同方式。 当我给出答案时,我认为他指的是两个不同的文件。 我能提供的唯一另一个答案是,这是个人喜好的问题。

Incorrect: Part of the problem with the settings file approach is that it requires the app to be in a specific place and the settings file to be in the correct place. If one of these gets accidentally moved, then the settings can be lost for good, same as if it gets deleted. There could also be the issue of multiple settings files with the same name in the folder, causing one to be overwritten. This is particularly bad if the software is dependent on the settings file. These problems aren't nearly as pronounced with installed software, but in software which isn't installed, but simply downloaded and run it can be a major issue. Imagine if someone downloaded several of these programs to the same directory and they all used the same settings file name.

EDIT: As discussed with the person who asked the question, this answer is incorrect because the question is referring to two different ways of saving to a settings file. When I gave my answer, I was thinking that he was referring to two different files. About the only other answer I can provide is that it's a matter of personal preference.

绻影浮沉 2024-07-31 22:22:49

我还想指出 app.Config 和“设置”窗口之间的一个区别。
如果您在“设置”下保存任何内容,则设置将保存在以下位置。

Win XP: c:\Documents and Settings\%Username%\Local Settings\Application Data{Publisher Name}\

Win 7: C:\Users\%Username%\AppData\Local{Publisher Name}\

而 app.config 设置仅保存在配置文件中。

I also would like to point out one difference between app.Config and Settings window.
If you save anything under Settings then the settings will be saved on following locations.

Win XP: c:\Documents and Settings\%Username%\Local Settings\Application Data{Publisher Name}\

Win 7: C:\Users\%Username%\AppData\Local{Publisher Name}\

While the app.config settings are saved on in the config file only.

一念一轮回 2024-07-31 22:22:48

我认为两者之间最大的区别是应用程序无法更改app.config中的值。 这些值是在运行时读取的,并且没有内置支持将新值写入配置文件。

可以使用Save()命令更改设置文件。

对设置文件的内置支持的一个主要问题是设置文件的存储位置。 如果您查看 APPDATA 文件夹,您会发现有一个包含公司名称的文件夹,然后是一个包含产品名称的子文件夹,然后是一个包含半随机名称和版本信息的子文件夹。

每当您发布新版本时,由于设置文件的存储位置,它都不会找到以前版本中的设置文件。 也无法更改设置文件的存储位置。

我在一个项目中使用了它,发现创建我自己的使用 XML 文件进行设置的 AppSettings 类更有用。 我可以控制文件的格式和位置。

I believe that the biggest difference between the two is that the application cannot change the values in app.config. Those values are read at runtime and there's no built-in support for writing new values to the configuration file.

Settings files can be changed using the Save() command.

One major problem with the built-in support for Settings files is where the settings file is stored. If you look at your APPDATA folder, you'll see that there is a folder for the name of the company, then a sub-folder with the name of the product, then a sub-folder with a semi-random name and version info.

Whenever you release a new version, it won't locate the Setting file from the previous version because of where the Setting file is stored. There's also no way to change where the Setting file is stored.

I used it in one project and found it much more useful to create my own AppSettings class that uses an XML file for settings. I have control over the format and location of the file.

不必在意 2024-07-31 22:22:48

还有第三种方法,也是比 AppSettings 或 Properties 更好的方法:自定义配置部分。 相对于 AppSettings 的优点:

1) 强类型访问

2) 针对架构和表单的内置验证机制。

3)基于POCO,使其具有高度可测试性——只需新建您自己的测试配置。

4)不依赖魔法弦。 并不是说您不能轻松地将 AppSettings 包装在类中并以这种方式访问​​它,但 95% 的开发人员不这样做。

5) 一旦您获得十几个左右的设置,配置中的 XML 就会变得更加容易理解。 恕我直言,它也比属性位更容易理解。

6) 不依赖 Visual Studio 来使其正常工作。

诚然,我从未真正使用过属性,因为我首先接触了自定义配置部分。

顺便说一句,如果您还没有听说过这些,那么您仍然每天都在使用它们——您认为 .NET 配置文件中的标准配置部分是什么?

_____澄清部分

首先,其中大部分内容是针对 AppConfig 方法的,重读后我并不清楚。 我认为我们通常站在同一边——避免使用松散的名称值包进行配置,因为它太容易损坏。 我还应该补充一点,我是一个网络人员,所以对我来说,用户设置是存在于应用程序层而不是配置中的东西。

1)确实,属性和自定义配置部分都具有强类型访问。 应用程序设置没有。 AppSettings 不好,Props/CC 好。

2)当加载配置节时,如果没有暴露必要的信息或不正确的信息,应用程序将抛出配置异常。 就像你搞乱了 app.config 或 web.config 一样。 我还没有使用更多的验证基础设施,因为我喜欢尽早失败或根本不失败。

3) POCO 是普通的旧 C# 对象,以防有人错过了过去几年的情况。 无论如何,由于配置设置是装饰性的并通过属性声明,因此您的配置设置可以轻松测试,因为您只需要指定一个新的 MyConfigSection() 并根据需要设置属性等。 据我了解属性,您需要有配置文件,其中包含正确的 xml,否则您就是 sol. 其他优点是自定义配置部分可以与您可以使用 POCO 执行的所有其他巧妙操作一起使用,例如接口和依赖项注入。

4)确实,Properties 和自定义配置部分都是强类型的,而 AppSettings 则不是。 AppSettings 不好,Props/CC 好。

5)真正针对AppSettings。 我继承了一些应用程序,在配置中只有 2 页 。 与属性吐出的 xml 乱码相比,这很容易。 另一方面,您的自定义配置部分可能相当语义化且不言自明,因为您正在声明部分名称和属性。 我可以很容易地在生产服务器上的记事本中编辑它,而不必担心在紧要关头搬起石头砸自己的脚。

因此,除了没有基于 VS 的属性网格(我认为这是一件坏事——为什么需要网格来编辑配置?)之外,与属性相比,自定义配置部分有很多优点,并且没有真正的缺点。 自定义配置部分和属性都比 AppSettings 具有巨大的优势。

There is a third, and much, much better way than either AppSettings or Properties: custom configuration sections. Advantages over AppSettings:

1) Strongly typed access

2) Built-in validation mechanisms for both the schema and the form.

3) POCO based making it highly testable--just new up your own test config.

4) No reliance on magic strings. Not that you can't easily wrap AppSettings in a class and access it that way, but 95% of developers don't.

5) XML in the configuration becomes much more intelligible once you get to a dozen or so settings. Also much more intelligible than the Propterties bits IMHO.

6) No reliance on visual studio to make it work well.

Granted, I never really used properties because I got my hands on custom configuration sections first.

BTW, if you haven't heard of these, you are still using them every day--what do you think the standard config sections in the .NET configuration files are?

_____CLAIRIFICATION SECTION

First, the bulk of this was aimed towards the AppConfig approach, and re-reading I wasn't clear. I think we're generally on the same side--avoid the loose name-value bag for configuration because it breaks too easily. I should also add that I am a web guy, so user settings are something that lives in the application layer, not in config, to me.

1) True, both Properties and custom config sections have strongly typed access. AppSettings don't. AppSettings bad, Props/CC good.

2) When you load a configuration section, the application will throw a configuration exception if it does not expose necessary information or improper information. Same as when you muck up a app.config or web.config. There is a bit more validation infrastructure I haven't used because I like to fail hard and early or not at all.

3) POCO is plain old C# object in case anyone missed the last few years. Anyhow, because config settings are decorative and declared via attributes, your config settings can be easily tested because you just need to delcate a new MyConfigSection() and set properties, etc, as appropriate. As I understand properties, you need to have the configuration file there with the right xml in it or you are sol. Other advantage is custom config sections work with all the other neat stuff you can do with POCOs, like interfaces and dependency injection.

4) True, both Properties and custom config sections are strongly typed, AppSettings are not. AppSettings bad, Props/CC good.

5) Really aimed at the AppSettings. I've inherited a few apps with litterally 2 pages of <add name="foo" value="bar" /> in the config. That is easy compared to the xml jibberish that properties spit out. On the other hand, your custom config section can be rather semantic and self-explanitory because you are declaring the section names and attributes. I can pretty easily edit it in notepad on the production server and not be too nervous about shooting myself in the foot in a pinch.

So, outside of not having a VS-based property grid (which I would contend is a bad thing--why do you need a grid to edit configuration?), custom config sections have alot of advantages and no real disadvantages compared to Properties. Both custom config sections and properties have massive advantages over AppSettings.

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