[英]How to dynamically configure an application?
When I say "configure" I mean where to save those values that could change very often (constants values like taxes rates or something similar) and then when you need to change them you don't want to re-compile your application. 当我说“配置”时,我的意思是保存那些可能经常更改的值(常量值,如税率或类似值),然后当您需要更改它们时,您不想重新编译您的应用程序。
Where to save those values? 在哪里保存这些值? Database?
数据库? XML File?
XML文件? Flat File?
平面文件?
It depends on how often these change and who or what changes them. 这取决于这些变化的频率以及改变它们的人或者是什么。 For some application specific settings, it's best to use an XML or config file, where the developers are the ones responsible for updating it.
对于某些特定于应用程序的设置,最好使用XML或配置文件,开发人员负责更新它。 For other "businessy" values (like exchange rates, tax rates, etc), it's best to keep them in the database and provide a UI for users (not developers) to update.
对于其他“商业”价值(如汇率,税率等),最好将它们保存在数据库中,并为用户(而非开发人员)提供更新的UI。
It also depends on how many apps depend on this value, for example, if several applications depend on some setting (such as email server addres), it's best to put it in a database since it'll be easily accessible from any machine where the app is running. 它还取决于有多少应用程序依赖于此值,例如,如果多个应用程序依赖于某些设置(例如电子邮件服务器地址),最好将其放在数据库中,因为它可以从任何机器上轻松访问应用正在运行。
I use INI files for potentially user-configurable files, and BIN files for data that save session state between runs. 我将INI文件用于潜在的用户可配置文件,将BIN文件用于在运行之间保存会话状态的数据。
But, it is very dependent upon what type of application you are developing. 但是,它非常依赖于您正在开发的应用程序类型。
it depends on how your app is architecture. 这取决于您的应用程序是如何构建的。 you could design your app in such way that you could change the location of you configuration.
您可以设计应用程序,以便更改配置的位置。 by just injecting the provider.
只需注入提供者。
Normally I use Ini files or XML if the data is structured. 通常,如果数据是结构化的,我使用Ini文件或XML。
For applications that already use a database and you don't want to have the user to change the data easily, you can use the database. 对于已经使用数据库并且您不希望用户轻松更改数据的应用程序,您可以使用该数据库。
I almost never use binary data unless you want to obfuscate the data for the user. 除非您想为用户模糊数据,否则我几乎从不使用二进制数据。
Regardless of app, you're probably going to have at least 3 sources of configuration data: 无论应用程序如何,您可能至少会拥有3个配置数据源:
Generally, anything that changes at run-time should go in the database. 通常,在运行时更改的任何内容都应该放在数据库中。 Anything that is sensitive and rarely changing should go into the config files, and any hacks should go on the command line (--[no]enable-bug-287438-hack can be very handy when you need it).
任何敏感且很少改变的东西都应该进入配置文件,任何黑客攻击都应该在命令行上进行( - [no] enable-bug-287438-hack在你需要时非常方便)。
I prefer the simplicity of a flat ini
file. 我更喜欢简单的
ini
文件。 Here's an example Setting
class that you might find useful. 这是一个您可能觉得有用的
Setting
类示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.