.NET 中的类型化配置值/变量

发布于 2024-08-21 15:46:37 字数 1082 浏览 5 评论 0原文

我们的应用程序中使用了很多配置文件。每个客户至少有 100 个不同的 xml 文件,其中包含至少 50-80 个名称/值对的配置,并且所有这些文件都经常更改(至少每月)。

配置文件看起来与下面类似,

  <property id="url"  value="s123"/>
  <property id="input-element-name"  value="name" />
  <property id="input-element-xpath" value="//body;//form;//table[3];" />

目前,为了从 xml 文件中读取这些值,我们使用 XmlReader 等,并在应用程序启动后将其存储在缓存中(当我们的客户群增加时,明天这可能会很大)。为了访问属性 ID,我们在静态类中创建了 const 变量以避免拼写错误等,

当我们配置较少时,这种方法非常有用。现在,维护并向此配置文件添加任何新内容是很痛苦的。需要进行重建等,只是消除了将可配置值保留在代码之外的概念。

我只是想知道 .NET 领域的最新发展(例如 M 语言、IronPython)是否可以帮助在配置文件更改时按需动态键入和编译配置值。我们对 xml 格式的配置没有任何保留。

总之,我需要的是一些具有我们的配置值的 .XXX 文件,如果更改或添加了任何内容,这些文件应该自动编译并且可以在我们的应用程序中使用而无需创建常量等,我知道 .T 会发生类似的情况VS 中的文件。

我希望这是可行的..

更新

这个问题的想法似乎没有被正确理解,否则我就不会正确拼写。抱歉给您带来麻烦了。

自定义配置部分可能不适合此问题,原因是如果我添加任何新的名称/值对,则使用 ConfigurationSection 需要修改代码。但是,如果仅在名称部分映射到代码中的属性时更改值,则此方法将起作用。

我想知道如果我使用任何动态语言品种,例如 IronPython 或 M Language,我可以将所有 XML 移动到其中,并且在我的应用程序中我将能够看到对此文件的任何更改(使用“FileSystemWatcher”)并执行它读取新添加的配置名称/值对。

We have lot of configuration files used in our application. Atleast 100 different xml files for each customer containing at least 50-80 name/value pairs of configuration and all they are often change (atleast every month).

The configuration file looks something similar like below,

  <property id="url"  value="s123"/>
  <property id="input-element-name"  value="name" />
  <property id="input-element-xpath" value="//body;//form;//table[3];" />

Currently to read this values from xml file we use XmlReader et al and store it in a cache once the application starts (and this could be huge tomorrow when our customer base increases). To access the property ids we have created const variables in a static class to avoid typo etc.,

This approach was great when we had less configurations. Now, it is painful to maintain and add any new to this configuration file. Need to do rebuild etc., just kills the concept of keeping configurable values outside code.

I was just wondering if any recent developments in .NET space such as M language, IronPython could help here to have dynamically typed and compiled configuration values on demand when configuration file gets changed. We don't have any reservations in having xml format for configuration.

In summary, what I need is to have some .XXX files having our configuration values and if anything is changed or added that should be automatically compiled and can be used in our application without creating constants etc., I know something similar happens with .T files in VS.

I hope this could be doable ..

UPDATE

The idea of this question was not seems to be understood correctly or I wouldn't have spelled out correctly. Sorry for the trouble.

The custom configuration section may not appropriate for this problem, the reason is using ConfigurationSection requires code modification if I add any new name/value pair. However, this approach will work if only change values as name part is being mapped to property in the code.

I was wondering if i use any dynamic language breeds such as IronPython or M Language I could be able to move all XML to it and in my application I would able to see any changes to this file (using `FileSystemWatcher') and execute it to read the newly added configuration name/value pairs.

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

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

发布评论

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

评论(4

御守 2024-08-28 15:46:37

您可以实现任意数量的自定义 ConfigurationSections我喜欢。

这将允许您将配置模型建模为强类型对象。

You can implement as many custom ConfigurationSections as you'd like.

This will allow you to model your configuration model as strongly typed objects.

韬韬不绝 2024-08-28 15:46:37

我使用了 Rick Strahl 的类型化配置类 - 它有效对我来说非常好。

I've used this typed configuration class from Rick Strahl - it worked very well for me.

那片花海 2024-08-28 15:46:37

Vadi,外部配置的目的是避免因配置更改而重新编译的需要。

如果您提供的 xml 代表您需要使用的信息类型,则自定义 ConfigurationSection 将为您提供良好的服务并提供您想要的类型安全。

Vadi, the purpose of external configuration is to obviate the need to recompile for configuration changes.

If the xml you present is representative of the type of information you need to use, a custom ConfigurationSection would serve you nicely and provide the type safety you want.

北座城市 2024-08-28 15:46:37

请查看 System.Configuration 命名空间中的类。它们自 .NET 2.0 以来就已经存在,并为您提供对 XML 配置文件的强类型访问,并完成验证。

Please look at the classes in the System.Configuration namespace. They have been there since .NET 2.0, and give you strongly-typed access to XML configuration files, complete with validation.

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