创建连接文件的最佳实践

发布于 2024-11-24 12:32:35 字数 378 浏览 5 评论 0原文

我有一个应用程序,其中有一些缓存、队列和数据库的配置文件。

public class ServerConfiguration: ConfigurationSection
{
    [ ConfigurationProperty( FOO, DefaultValue = "", IsRequired = false ) ]
    public string FOO
    {
        get { return (string)this[FOO]; }
        set { this[FOO] = value; }
    }
}

这就是我对配置文件所做的事情,并且我还有一些继承层次结构。

您使用什么来处理配置?为此目的有哪些最佳实践?

I have an application which i have some configuration files for cache, queue, and database.

public class ServerConfiguration: ConfigurationSection
{
    [ ConfigurationProperty( FOO, DefaultValue = "", IsRequired = false ) ]
    public string FOO
    {
        get { return (string)this[FOO]; }
        set { this[FOO] = value; }
    }
}

this is what i do for config files and I also have some inheritance hierarchy.

What do you use to handle configurations and what are some best practices for this purpose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2024-12-01 12:32:35

我喜欢并广泛使用 Microsoft 配置库,但我尝试确保我的应用程序不依赖于它。这通常涉及让我的配置部分实现一个接口,因此您的示例如下所示:

public class ServerConfiguration : ConfigurationSection, IServerConfiguration
{
    [ ConfigurationProperty( FOO, DefaultValue = "", IsRequired = false ) ]
    public string FOO
    {
        get { return (string)this[FOO]; }
        set { this[FOO] = value; }
    }
}

public interface IServerConfiguration
{
    public string FOO { get; } //Unless I am updating the config in code I don't use set on the interface
}

现在,无论您在代码中使用您的配置,您只需要担心 IServerConfiguration 并且可以更改您的实现,而无需更改用法。有时,我只是在开发过程中从硬编码的类开始,只有当我实际上需要在不同的环境中具有不同的值时才将其更改为配置部分。

如果您使用配置部分,您还依赖于 ConfigurationManager。我通过使用 IConfigurationProvider[T] 从我的代码中隐藏了这一点,其中 T 是 IServerConfiguration,您可以在我的博客上的配置忽略下看到此示例。

http://bronumski.blogspot.com/search/label/Configuration

I love and use the Microsoft configuration library extensively but I try to make sure that my applications are not dependent on it. This usually involves having my configuration section implement an interface, so your example would look like:

public class ServerConfiguration : ConfigurationSection, IServerConfiguration
{
    [ ConfigurationProperty( FOO, DefaultValue = "", IsRequired = false ) ]
    public string FOO
    {
        get { return (string)this[FOO]; }
        set { this[FOO] = value; }
    }
}

public interface IServerConfiguration
{
    public string FOO { get; } //Unless I am updating the config in code I don't use set on the interface
}

Now where ever you use your configuration in your code you only need to worry about IServerConfiguration and you can change your implementation without having to change the usages. Sometimes I just start of with a hard coded class during development and only change it to a configuration section when I actually need to have different values in different environments.

If you are using a configuration section you are also dependent on the ConfigurationManager. I have hidden this from my code by using an IConfigurationProvider[T] where T would be IServerConfiguration, you can see an example of this on my blog under configuration ignorance.

http://bronumski.blogspot.com/search/label/Configuration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文