简体   繁体   English

如何动态配置应用程序?

[英]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个配置数据源:

  1. Command line flags, usually for bootstrapping your run-time environment, eg, finding config files, setting debug flags, include paths, class paths, etc 命令行标志,通常用于引导运行时环境,例如,查找配置文件,设置调试标志,包括路径,类路径等
  2. Config files, potentially more than one that may override each other. 配置文件,可能不止一个可能互相覆盖的文件。 These usually boot strap your application: connection strings, cache settings, build-specific settings, etc 这些通常会引导您的应用程序:连接字符串,缓存设置,特定于构建的设置等
  3. Control data in a database. 控制数据库中的数据。 Things like timezones, conversion rates, stable display values, etc. This data should also be versioned in the database (as in, a "Data Version" field, not living in a version control system). 时区,转换率,稳定显示值等等。此数据也应在数据库中进行版本控制(如“数据版本”字段,不在版本控制系统中)。 Versioning it will save a lot of headaches when you find you need to change a setting for a new release, but the old release will break if you change it. 当您发现需要更改新版本的设置时,对它进行版本控制会省去很多麻烦,但如果您更改它,旧版本将会中断。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM