Fluent 和 XML 配置的优缺点
如果您要使用一些第三方库(logging、orm、di 等等)启动一个新项目,您是否愿意通过使用流畅的接口或 XML 文件进行编程来配置所有这些库?
您会使用仅支持这些选项之一(Fluent 或 XML)的库吗?或者您更喜欢那些可以让您在各种配置策略之间进行选择的库。
对于那些喜欢代码的人,可以将此作为一个假设的示例(用 C# 编写)。
这是 Fluent 配置:
LogConfiguration.ConfigureStorage()
.ForDatabase("CommonDB")
.AsMsSqlDatabase()
.WithConnectionString("server=(local); database=Northwind; Integrated Security=true;")
.AsDefault();
这是 XML 配置:
<logging>
<database name="CommonDB" provider="MSSQL" connString="server=(local); database=Northwind; Integrated Security=true;" default="true" />
</logging>
最后,Fluent 和 XML 配置的优缺点是什么?
到目前为止,我们已经做到了:
代码中的流畅配置
优点
- 由编译器评估强类型
- 有条件配置
缺点
- 构建后无法重新配置
>XML 配置
优点
- 部署后能够轻松更改
缺点
- XML 冗长
- 更容易出现输入错误
If you we're about to start a new project with some third party libraries (logging, orm, di, whatever), would you prefer to configure all this libraries via programming with fluent interfaces or with XML files?
Would you use a library that only supports one of these options (Fluent or XML)? Or do you prefer libraries that gives you the possibility to choose between a variety of configuration strategies.
For those who like code, take this as a hypothetical example (written in C#).
This is Fluent Configuration:
LogConfiguration.ConfigureStorage()
.ForDatabase("CommonDB")
.AsMsSqlDatabase()
.WithConnectionString("server=(local); database=Northwind; Integrated Security=true;")
.AsDefault();
This is XML Configuration:
<logging>
<database name="CommonDB" provider="MSSQL" connString="server=(local); database=Northwind; Integrated Security=true;" default="true" />
</logging>
Finally, what are the Pros and Cons of Fluent and XML Configuration?
Until now, we've come to this:
Fluent Configuration in Code
Pros
- Strongly typed evaluated by compiler
- Conditional configuration
Cons
- Unabled reconfigured after build
XML Configuration
Pros
- Ability to easily change after deploy
Cons
- XML is verbose
- More vulnerable to typing mistake
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我倾向于使用 xml 作为我可能想要更改构建后的属性(例如连接字符串、记录器)。
对于仅在开发期间更改的 NHibernate 映射等内容,我更喜欢代码中的强类型(编译)流畅配置。
I tend to use xml for attributes I might want to change post-build (such as connection strings, loggers)
I prefer strongly typed (compiled) fluent configuration in code for things such as NHibernate mappings that only change during development.
如果我对你的理解正确的话,你所说的“流利”是指代码吗?如果是这样,那么我肯定会选择 XML。如果你所说的流利是指纯文本文件,那么我仍然会选择 XML。
优点:
缺点:
目前我的所有项目都是基于 XML 的。
If I understood you correctly by fluent you mean in code? If so, then definitely I'd choose XML. If by fluent you meant pure text files, then still I'd choose XML.
Pros:
Cons:
Currently all my projects are XML based.
我想说 XML 配置可能比 Java 配置更简洁,甚至更简单。
看看我对 Spring Integrations 中 XML 和 Java DSL 上相同配置的比较 < code>wireTap config
另外,经验不足的程序员可能更熟悉 XML 配置。因此,将来维护此类配置的工作量可能会减少。
I would say that XML configs may be less verbose and even simpler than Java configs.
Look on my comparison of the same config on XML and on Java DSL in Spring Integrations's
wireTap
configAlso XML configs may be more familiar to less experienced programmers. So efforts for maintain such configs in the future may be less.