最佳实践创建连接文件 [英] Best practices to create connection files
问题描述
我有我有高速缓存,队列和数据库中的一些配置文件的应用程序。
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?
推荐答案
我喜欢并使用Microsoft配置库广泛,但我试图确保我的应用程序不依赖于它。这通常需要有我的配置部分实现一个接口,让您的例子看起来像:
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
}
现在在任何你用你的配置在code,你只需要担心IServerConfiguration,你可以改变你的实现,而无需改变用途。有时候,我刚开始用硬codeD级的开发过程中,只有将其更改为一个配置节当我真正需要有不同的价值观在不同的环境。
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.
如果您使用的是配置部分,您也取决于ConfigurationManager中。我已经通过使用IConfigurationProvider [T],其中T是IServerConfiguration,你可以看到在我的下配置的无知博客这样的例子隐藏这从我的code。
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
这篇关于最佳实践创建连接文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!