Best practices to create connection files

做~自己de王妃 提交于 2019-12-05 12:20:59

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!