Introduction to configuration wrapper

/* By Dylan SUN */

In your applications, you are certainly using the configuration sections like appSettings for custom configurations, connectionStrings for database binding and other sections like WCF bindings, etc.

To make your application more flexible for unit testing, you could use a configuration wrapper to control the access to the configuration file.

You can create an interface to expose some methods to access different configurations.

This is two methods to retrieve the appSettings and connectionStrings by the configuration key.

    public interface IConfigurationReader
    {
        string GetAppSettings(string key);

        string GetConnectionString(string key);
    }

Here are their implementation.

    public class ConfigurationReader : IConfigurationReader
    {
        public string GetAppSettings(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }

        public string GetConnectionString(string key)
        {
            return ConfigurationManager.ConnectionStrings[key].ConnectionString;
        }
    }

You can register the interface and its implementation with dependency injection pattern using UnityContainer.

IUnityContainer container = new UnityContainer();

//register interface and its implementation
container.RegisterType<IConfigurationReader, ConfigurationReader>();

Resolve the interface and use the class to access the configuration value.

//resolve the interface
var configurationReader = container.Resolve<IConfigurationReader>();

//access GetAppSettings method to get the value
var value = configurationReader.GetAppSettings("hello");
Console.WriteLine(value);

You can even centralize the configuration keys in a static class to facilitate the configuration key management.

I hope you find this article useful! Thanks.

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。