简体   繁体   English

默认值:硬代码还是.config文件?

[英]Default values: hard-code, or .config file?

I'm working on an issue-tacking application, where website users enter issues which are sent over a WCF service to an administration console, where support agents begin handling the issue. 我正在开发一个解决问题的应用程序,其中网站用户输入问题,这些问题通过WCF服务发送到管理控制台,在该控制台中,支持代理开始处理该问题。

As issues come over the wire, they are grouped by a projectID and a categoryID 随着问题的出现,它们会按projectID和categoryID分组

All issues coming from a specific website, should have the same default Project and CategoryID's. 来自特定网站的所有问题均应具有相同的默认Project和CategoryID。

Problem: 问题:

It is unknown at this point, what those default values should be since projects and categories are entered by support agent managers sometime in the future within the admin console. 目前尚不清楚这些默认值应该是多少,因为将来某个时候支持代理经理会在管理控制台中输入项目和类别。 (None of this is yet deployed, and a meeting between myself and the support team to hammer out these details is unlikely) (这些都尚未部署,我和支持团队之间召开会议以敲定这些细节的可能性不大)

Question: 题:

What is the best way to handle these default values? 处理这些默认值的最佳方法是什么? What I've considered so far: 到目前为止,我已经考虑了:

1) Hard-code them. 1)对它们进行硬编码。 The problem with hard-coding, is that it will be difficult to change the values after deployment. 硬编码的问题在于,部署后很难更改值。

2) Stick em' in a .config file. 2)将em贴在.config文件中。 This is flexible, and will allow things to be changed easily after deployment, but I hardly think it's elegant (or at least it just feels that way to me) 这很灵活,可以在部署后轻松更改,但是我几乎不觉得它很优雅(或者至少对我来说就是这种感觉)

If you've had any similar experiences, or just good insight and advice, I'd love to hear it! 如果您有任何类似的经验,或者只是很好的见识和建议,我很想听听! Thank you, 谢谢,

You might even consider going one step further and storing your defaults in the database. 您甚至可以考虑更进一步,将默认值存储在数据库中。 This will provide flexibility of configuration and centralized management of those defaults. 这将提供配置的灵活性以及这些默认值的集中管理。

Personally, the approach I might often take is to have my configuration in code, but have the ability to override the defaults by supplying new values in the configuration file or in the Db. 就个人而言,我可能经常采用的方法是在代码中包含我的配置,但是能够通过在配置文件或Db中提供新值来覆盖默认值。 Why do I go this far? 我为什么要走这么远? Well this way I can deploy with minimal configuration and then provide overrides if and when required. 这样,我可以用最少的配置进行部署,然后在需要时提供替代。

Since there is no way that you know what these values should be at deployment time, you should have them in configuration. 由于无法知道在部署时这些值应该是什么,因此应在配置中进行设置。 You may find it inelegant, but with values that may need to change without code changes, this is the only option. 您可能会发现它不太雅致,但是如果不更改代码就需要更改其值,那么这是唯一的选择。

Unless you know that deployment is fast and easy and that any code changes can be deployed quickly and without error. 除非您知道部署是快速且容易的,并且任何代码更改都可以快速且没有错误地部署。

您也可以将它们存储在存储问题跟踪数据的数据库中。

First, hard-code reasonable defaults to maintain resiliency. 首先,硬编码合理的默认值以保持弹性。 No one likes a program which is made useless by the absence of not so strictly necessary text file. 没有人喜欢这样一种程序,即由于缺少不是那么严格必需的文本文件而变得无用的程序。

Second, a config file is not at all an inelegant way to handle custom configuration. 其次,配置文件根本不是处理自定义配置的简便方法。 Depending on the environment you're deploying to, providing a gui config tool is another option. 根据您要部署到的环境,提供gui配置工具是另一种选择。 ie are end users going to be changing configs on thy fly? 即最终用户是否会随时更改配置? Or are sys admins going to be making planned changes? 还是系统管理员要进行计划的更改?

Putting them in the configuration is probably the best choice. 将它们放在配置中可能是最佳选择。 If you're looking to have a nice way to access them you can create a static class that acts as an accessor or use something like dependency injection. 如果您正在寻找一种访问它们的好方法,则可以创建一个充当访问器的静态类,或使用诸如依赖注入之类的方法。

Example accessor class: 访问器类示例:

public static class DefaultSupportValues
{
    public static string ProjectID
    {
        get
        {
            return ConfigurationManager.AppSettings["DefaultProjectId"];
        }
    }
}

I think a third option which you have not listed is to have them in a Database. 我认为您没有列出的第三个选择是将它们存储在数据库中。 There are various caching strategies you can employ to address performance concerns. 您可以采用多种缓存策略来解决性能问题。 The benefit of this approach is that you could host your application on one or more servers and not have it be a pain to update config changes for them. 这种方法的好处是您可以将应用程序托管在一个或多个服务器上,而不必为它们更新配置更改。

In my experience, this has been the best approach for these cases. 以我的经验,这是处理这些情况的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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