的appSettings VS的applicationSettings的优点和缺点(.NET的app.config) [英] Pros and cons of appSettings vs applicationSettings (.NET app.config)

查看:192
本文介绍了的appSettings VS的applicationSettings的优点和缺点(.NET的app.config)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发.NET Windows窗体应用程序,我们有那些的App.config 标签之间的选择来存储的配置值。哪一个更好呢?

 <结构>

  <! - 选择1  - >
  <的appSettings>
    <添加键=RequestTimeoutInMilliseconds值=10000/>
  < /的appSettings>

  <! - 选择2  - >
  < configSections>
    < sectionGroup名=的applicationSettingsTYPE =System.Configuration.ApplicationSettingsGroup,系统,版本= 2.0.0.0,文化=中性公钥= b77a5c5612342342>
        <节名称=Project1.Properties.SettingsTYPE =System.Configuration.ClientSettingsSection,系统,版本= 2.0.0.0,文化=中性公钥= b77a5c5612342342requirePermission =FALSE/>
    < / sectionGroup>
  < / configSections>
  <的applicationSettings>
    < Project1.Properties.Settings>
      <设定名=TABLEAserializeAs =字符串>
        <价值> TABLEA< /值GT;
      < /设置>
    < /Project1.Properties.Settings>
  < /的applicationSettings>

< /结构>
 

解决方案

基本<的appSettings> 是容易应付过去 - 只拍了<添加键=...值=.../> 条目,你就大功告成了。

缺点是:有没有类型检查,例如:你不能安全地假设你想有配置真的是一个数字的号码 - 有人可以把一个字符串转换成该设置.....你刚刚接触它作为 ConfigurationManager中[(关键)] 然后就看你知道你正在处理的是什么。

此外,随着时间的推移,在<的appSettings> 可以得到相当令人费解和杂乱,如果很多你的应用程序的部分开始把东西在里面(记得旧窗.ini文件: - ?))

如果可以的话,我会preFER和推荐使用自己的配置节 - 用.NET 2.0,它真的变得非常容易,这样一来,您可以:

  • 一)定义在code的配置设置,并让它们类型安全 并检查
  • b)你可以干净地从众人中分离的的设置 别人的。你可以重复使用您的配置code,太!

有一系列你真的好文章,对非神秘化$ C $的CProject的.NET 2.0的配置系统:

  1. 揭开.NET 2.0配置

    <奥秘/ LI>
  2. 解码.NET 2.0配置

    <奥秘/ LI>
  3. 开裂的.NET 2.0配置

    <奥秘/ LI>

强烈推荐!乔恩Rista做了伟大的工作,解释.NET 2.0。

配置系统

When developing a .NET Windows Forms Application we have the choice between those App.config tags to store our configuration values. Which one is better?

<configuration>

  <!-- Choice 1 -->
  <appSettings>
    <add key="RequestTimeoutInMilliseconds" value="10000"/>
  </appSettings>

  <!-- Choice 2 -->
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5612342342" >
        <section name="Project1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5612342342" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <Project1.Properties.Settings>
      <setting name="TABLEA" serializeAs="String">
        <value>TABLEA</value>
      </setting>
    </Project1.Properties.Settings>
  </applicationSettings>

</configuration>

解决方案

The basic <appSettings> is easier to deal with - just slap in a <add key="...." value="..." /> entry and you're done.

The downside is: there's no type-checking, e.g. you cannot safely assume your number that you wanted to configure there really is a number - someone could put a string into that setting..... you just access it as ConfigurationManager["(key)"] and then it's up to you to know what you're dealing with.

Also, over time, the <appSettings> can get rather convoluted and messy, if lots of parts of your app start putting stuff in there (remember the old windows.ini file? :-)).

If you can, I would prefer and recommend using your own configuration sections - with .NET 2.0, it's really become quite easy, That way, you can:

  • a) Define your configuration settings in code and have them type-safe and checked
  • b) You can cleanly separate YOUR settings from everyone else's. And you can reuse your config code, too!

There's a series of really good articles on you to demystify the .NET 2.0 configuration system on CodeProject:

  1. Unraveling the mysteries of .NET 2.0 configuration

  2. Decoding the mysteries of .NET 2.0 configuration

  3. Cracking the mysteries of .NET 2.0 configuration

Highly recommended! Jon Rista did a great job explaining the configuration system in .NET 2.0.

这篇关于的appSettings VS的applicationSettings的优点和缺点(.NET的app.config)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆