[英]Best way to manage a Configuration File?
A colleague and I were discussing best practices for managing a Configuration file, and we wanted to get some further feedback from others. 我和一位同事正在讨论管理配置文件的最佳实践,我们想从其他人那里获得一些进一步的反馈。
Our goal is for the configuration-file to specify what action should be taken, when certain events occur. 我们的目标是配置文件指定在发生特定事件时应采取的操作。
The 2 options that we are debating: 我们正在辩论的两个选项:
In the config-file, specify the class-path of the class, which implements the action to be taken (eg: "ActionToTake" : "com.company.publish.SendEveryoneAnEmailClass") . 在config-file中,指定类的类路径,该路径实现要采取的操作(例如:“ActionToTake”:“com.company.publish.SendEveryoneAnEmailClass”) 。 Inside the code, when this event is encountered, we can then do Class.forName(config.ActionToTake).newInstance().run() in order to invoke the specified action.
在代码内部,当遇到此事件时,我们可以执行Class.forName(config.ActionToTake).newInstance()。run()以调用指定的操作。
In the config-file, specify in a human-readable-phrase, the action that should be taken (eg: "ActionToTake" : "SendEveryoneAnEmail") . 在配置文件中,在人类可读短语中指定应采取的操作(例如:“ActionToTake”:“SendEveryoneAnEmail”) 。 Inside the code, when this event is encountered, we can then parse config.ActionToTake, and perform a mapping that translates this to action implementation (eg: new SendEveryoneAnEmailClass().run())
在代码内部,当遇到此事件时,我们可以解析config.ActionToTake,并执行将其转换为动作实现的映射(例如:new SendEveryoneAnEmailClass()。run())
We are currently a very small team, and the only people reading/using this config file currently, is our team of software devs. 我们目前是一个非常小的团队,目前唯一阅读/使用此配置文件的人是我们的软件开发团队。 But it's unclear if this will continue to be true in future.
但目前还不清楚这是否会继续成为现实。
Reasoning behind option 1: Anyone reading the config file will explicitly and immediately know what class will get invoked, and where it's implemented. 选项1背后的推理:任何读取配置文件的人都会明确地立即知道将调用哪个类,以及它的实现位置。 This also allows for the action-class to be implemented/imported from a completely separate JAR file, without recompiling/changing our code.
这也允许从完全独立的JAR文件实现/导入动作类,而无需重新编译/更改我们的代码。
Reasoning behind option 2: The config file should be a high level description of user-intent, and should not contain implementation details such as specific class names & package paths. 选项2背后的推理:配置文件应该是用户意图的高级描述,并且不应包含实现细节,例如特定的类名和包路径。 Refactoring of class/package names can also be done without having to make config file changes.
也可以在不必更改配置文件的情况下重构类/包名称。
Thoughts on which of these 2 design philosophies is preferred for configuration files? 关于配置文件首选哪两种设计理念?
1st option's advantage is, as jas noticed, the ability to 'link' code in the future. 正如jas所注意到的,第一个选项的优势在于将来能够“链接”代码。 It's a real advantage only if you sell/distribute your software as a closed sourced package or if you plan to hot-swap behavior on production.
只有当您将软件作为封闭源代码包销售/分发或者您计划在生产中进行热交换行为时,这才是真正的优势。 You've already pointed out the cons - refactoring
你已经指出了重构 - 重构
2nd option: 第二种选择:
send email
then you failed. send email
则表示您失败了。 send-everyone-an-email
is as good as SendEveryoneAnEmail
. send-everyone-an-email
和SendEveryoneAnEmail
一样好。 Every developer will know what will happen. LaunchRockets
. LaunchRockets
混淆。 Your code can find class based on some text, not necessarily the full qualified name. Consider also another possibility: do the configuration in code. 还要考虑另一种可能性:在代码中进行配置。 If you don't want to recompile the package, you can use scripting language (groovy).
如果您不想重新编译包,则可以使用脚本语言(groovy)。 It lets you create very readable dsl, and you will have refactoring.
它允许您创建非常易读的dsl,并且您将进行重构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.