.net 中基于脚本的配置?

发布于 2024-10-10 09:58:13 字数 1074 浏览 3 评论 0原文

web.config/app.config 的缺点之一是它到处都是魔术字符串,因为它只是一个 XML 文件。

PHP 或 Ruby 等解释性语言的优点是配置只是执行的代码。在 .net 中,在代码中执行操作需要重新部署,这违背了目的。

现在,在我基于 Boo 或 PowerShell 构建自己的 web.config 替换之前想知道是否有现成的?

举个例子,这里的代码很简单,但在 web.config 中却非常困难/尴尬:

IList<TimeZoneInfo> allowedTimeZones =
         System.TimeZoneInfo.GetSystemTimeZones()
           .Where(tz => tz.DisplayName.Contains("Central"));

基本上每次您需要执行复杂的操作来创建对象时,基于 XML/字符串的配置都会变得极其混乱。

例如,如果 allowedTimeZones 确实应该是“以‘Central’开始,但不包含‘Europe’”,那么您就开始在 XML 之上构建自己的 DSL(规则

如果配置文件中有可执行代码,我只需将其修改为

IList<TimeZoneInfo> allowedTimeZones =
         System.TimeZoneInfo.GetSystemTimeZones()
           .Where(tz => tz.DisplayName.StartsWith("Central") &&
                  !tz.DisplayName.Contains("Europe"));

当然,因为 C# 不是脚本语言,所以此示例将是采用 Boo 或 PowerShell 等只能由应用程序执行的语言。

One of the downsides of web.config/app.config is that it's just Magic Strings everywhere, since it's just an XML file.

Interpreted languages like PHP or Ruby have the advantage that the configuration is just code that is executed. In .net, doing stuff in code requires a redeployment, which defeats the purpose.

Now, before I build my own web.config replacement based on Boo or PowerShell I wanted to know if there is an existing one?

As an example, this here is trivial in code but really hard/awkward in web.config:

IList<TimeZoneInfo> allowedTimeZones =
         System.TimeZoneInfo.GetSystemTimeZones()
           .Where(tz => tz.DisplayName.Contains("Central"));

Basically every time you need to perform complex actions to create an object, XML/String based configurations become extremely messy.

For example, if the allowedTimeZones really should be all that "Start With 'Central' but do not contain 'Europe'", then you start building your own DSL essentially on top of XML (Rules <add action="startswith" value="Central"/> <add action="doesnotcontain" value="Europe/>). In code, I then need to loop through all of those and either have a large switch statement that translates every line into code, or I could use LINQ-Method Names as Names and use Reflection to invoke them.

If the config file where executable code, I would just modify it to

IList<TimeZoneInfo> allowedTimeZones =
         System.TimeZoneInfo.GetSystemTimeZones()
           .Where(tz => tz.DisplayName.StartsWith("Central") &&
                  !tz.DisplayName.Contains("Europe"));

Of course, since C# is not a scripting language this example would be in a language like Boo or PowerShell that can just be executed by the application.

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

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

发布评论

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

评论(2

柳絮泡泡 2024-10-17 09:58:13

最终建立了我自己的。本质上,利用 App_Code 文件夹(它自动回收应用程序池,从而重新启动应用程序)并在 Application_Start 事件中使用一些反射来获取实现特定接口/基类的类并运行它们。

明显的免责声明:在 AppDomain 中执行随机代码可能是一个安全问题,因为它比修改 web.config 危险得多,但在受信任的环境中,这非常好。

Ended up building my own. Essentially, make use of the App_Code folder (which automatically recycles the Application Pool and thus restarts the application) and use a bit of Reflection in the Application_Start event to get classes that implement a certain interface/base class and run them.

Obvious Disclaimer: Executing random code in your AppDomain can be a security issue as it's far more dangerous than modifying the web.config, but in trusted environments it's quite nice.

初心 2024-10-17 09:58:13

我还没有见过这样的事情。正如您所说,您可以托管脚本语言,然后只需读取启动脚本。

当然,使用 IOC,您可以部署修改后的 DLL 并将 web.config 指向它们,这将是类似的,但需要进行一些编译(新的配置 DLL)来检查代码,而不是运行时脚本出现运行时错误。但对于所有 IOC 容器工作,与静态错误相比,出现运行时错误的可能性更大。

我想大多数人目前都将动态风险放在这两个桶中 - 可能存在细微错误的神奇配置文件或可能存在细微错误的神奇 IOC。您的建议是脚本,在解析它们之前您不会知道它们是否失败 - 放置故障排除的位置略有不同 - 不过,我可以看到好处。

I haven't seen anything like that. As you said, you could host a scripting language and then simply read a startup script.

Of course, with IOC, you could deploy modified DLLs and point the web.config at them, which would be similar but have a bit of compiling (of the new configuration DLL) to check the code instead of the runtime script having runtime errors. But with all IOC container work, you have more likelihood of runtime errors vs. static errors.

I guess most people are currently putting their dynamic risk in those two buckets right now - magic config files which can have subtle errors or magic IOC which can have subtle errors. Your suggestion would be scripts which you won't know if they fail until they get parsed - a slightly different place to put the troubleshooting - I can see the benefits, though.

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