Using SingleTagSectionHandler Instead Of appSettings

  1. While working with any kind of configuration files in .NET, be it Web.config in case of ASP.NET or App.config in case of Windows Forms applications, we very often use the appSettings section. We use it to store all kinds of simple configuration options. Options, that are too simple for us to implement a completely new SectionHandler type. There is however one problem with appSettings section not very expressive.

    What does it mean to be expressive? Consider the following example:

    <appSettings>
      <add key="source1user" value="user" />
      <add key="source1password" value="pass" />
    </appSettings>

    In simple cases, it may be acceptable, but what happens if more user keys are required for some reason? Maybe we need to access few different locations, each of which requires a username and password? We may use some kind of prefix for each key like in the above example, but that is not very elegant. What is key and value anyway?

    Another obvious problem here is when we need to have 2 parameters associated with a single logical functionality in the application, we need 2 entries in appSettings section.

    If we still don't want to implement a new SectionHandler type, we have very nice option left: SingleTagSectionHandler.

    MSDN describes it as: "Handles configuration sections that are represented by a single XML tag in the .config file". And that's about it. Unfortunately, (as usual) MSDN provides no example of how to use it. Fortunately it is quite simple:

    <configSections>
      <section name="remoteDataSource" type="System.Configuration.SingleTagSectionHandler" />
    </configSections>
    
    <remoteDataSource username="user" password="pass" url="http://remote/" />

    Using the newly declared section from the code is also easy:

    Hashtable remoteDataSource = 
    (Hashtable)WebConfigurationManager.GetSection("remoteDataSource");
    string username = (string)remoteDataSource["username"];
    string password = (string)remoteDataSource["password"];
    string url = (string)remoteDataSource["url"];

    Simple, yet useful.

    While working with any kind of configuration files in .NET, be itWeb.config in case of ASP.NET or App.config in case of Windows Formsapplications, we very often use the appSettings section. We use it tostore all kinds of simple configuration options. Options, that are toosimple for us to implement a completely new SectionHandler type. Thereis however one problem with appSettings section not very expressive.What does it mean to be expressive? Consider the following example:<appSettings> <add key="source1user" value="user" /> <add key="source1password" value="pass" /></appSettings>In simple cases, it may be acceptable, but what happens if more userkeys are required for some reason? Maybe we need to access fewdifferent locations, each of which requires a username and password? Wemay use some kind of prefix for each key like in the above example, butthat is not very elegant. What is key and value anyway?Another obvious problem here is when we need to have 2 parametersassociated with a single logical functionality in the application, weneed 2 entries in appSettings section.If we still don't want to implement a new SectionHandler type, we have very nice option left: SingleTagSectionHandler. MSDN describes it as: "Handles configuration sections that arerepresented by a single XML tag in the .config file". And that's aboutit. Unfortunately, (as usual) MSDN provides no example of how to useit. Fortunately it is quite simple:<configSections> <section name="remoteDataSource" type="System.Configuration.SingleTagSectionHandler" /></configSections><remoteDataSource username="user" password="pass" url="http://remote/" />Using the newly declared section from the code is also easy:Hashtable remoteDataSource = (Hashtable)WebConfigurationManager.GetSection("remoteDataSource");string username = (string)remoteDataSource["username"];string password = (string)remoteDataSource["password"];string url = (string)remoteDataSource["url"];Simple, yet useful.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值