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.

时间: 2024-10-17 01:15:14

Introduction to configuration wrapper的相关文章

Springfox Reference Documentation

1. Introduction The Springfox suite of java libraries are all about automating the generation of machine and human readable specifications for JSON APIs written using the spring family of projects. Springfox works by examining an application, once, a

kbuild-(directory)

00-INDEX - this file: info on the kernel build process kbuild.txt - developer information on kbuild kconfig.txt - usage help for make *config kconfig-language.txt - specification of Config Language, the language in Kconfig files makefiles.txt - devel

Linux Kernel的Makefile与Kconfig文件的语法

https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt Introduction ------------ The configuration database is a collection of configuration options organized in a tree structure: +- Code maturity level options | +- Prompt for developme

OpenStack Mixture HypervisorsDriver configure and implementation theory

通过本文,您将可以了解在 OpenStack 中如何进行混合 Hypervisor 的配置及其实现原理的基本分析.本文主要结合作者在 Nova 中的实际开发经验对 OpenStack 中混合 Hypervisor 场景的原理进行分析,同时介绍在实际的应用场景中如何进行配置. 通常,基于 OpenStack 的混合 Hypervisor 场景中的部署有三个关键步骤: 配置不同 Hypervisor 类型的 Compute 节点 标识 Image 的 Platform/Hypervisor 属性,即

转 Nexus启动报错:Unsupported major.minor version 51.0

转:http://www.th7.cn/Program/java/201408/269316.shtml Nexus启动报错: Unsupported major.minor version 51.0 1.遇到问题 今天搭建Nexus私服,出现了以前没遇到过的错误. a).采用console-nexus.bat开启报错: wrapper| --> Wrapper Started as Consolewrapper| Launching a JVM...jvm 1| java.lang.Unsup

CentOS安装nexus

1, Nexus 下载 Nexus 官方下载, 最新版下载: nexus-2.4.0-09-bundle.tar.gz Nexus war下载: nexus-2.4.0-09.war nexus有两种安装实现方式,一种是war包的方式,将它直接放在例如tomcat下就可以启用的,还有一种源码的方式,做为一个运维还是喜欢源码方式的说哈.开始吧... 2, Nexus 安装 解压tar.gz文件: #cd /home/tools #tar zxvf nexus-2.4.0-09-bundle.tar

重构Mybatis与Spring集成的SqlSessionFactoryBean(2)

三.代码重构 1.先使用Eclipse把buildSqlSessionFactory()方法中众多的if换成小函数 protected SqlSessionFactory buildSqlSessionFactory() throws IOException { Configuration configuration; XMLConfigBuilder xmlConfigBuilder = null; if (this.configuration != null) { configuration

《Pattern-Oriented Software Architecture, Patterns for Concurrent and Networked Objects》Vol.2 笔记

GoF的23种经典模式使得设计模式开始成为程序员的通用语言.<Pattern-Oriented Software Architecture, Patterns for Concurrent and Networked Objects> Volume 2主要总结了并行系统中四大类16种设计模式.这里是一个摘录整理. Service Access and Configuration Wrapper Facade 它将底层的系统级API包装成加面向对象的,统一的和可移植的接口.像Thread, Mu

nexus私服linux搭建问题

一.最近搭建nexus私服,从官网下载下来总是报503服务器无效,很是无奈,最后在网上找到一个可以用的 收藏起来,这里给大家共享一下 下载地址:http://pan.baidu.com/s/1kT3UJHl 二.解压之后会有两个文件夹,nexus-2.6.0-05和sonatype-work,我们进入第一个文件夹的bin目录来启动nexus . 1.进入启动目录,启动 cd nexus-2.6.0-05/bin/ ./nexus start 2.这时候提示: ******************