. NET how to customize config configuration file nodes

Source: Internet
Author: User
Tags connectionstrings

This article reproduced: http://www.cnblogs.com/lori/archive/2013/04/03/2997617.html

For small projects, configuration information can be configured through appsettings, and if the configuration information is too much, appsettings seems a bit messy, and when the developer calls, is not friendly, the node name is easy to write wrong, at this time, we have several solutions

1 Develop a configuration information persistence class for managing configuration information and providing object-oriented support2 using the. NET configsections, the configuration information is chunked managed, and the entity classes are provided, making it easy for developers to use it

This article is mainly about the second scenario, which consists of the entity class, the entity class factory and the configuration file three parts, see the code:

Entity class Design:

namespace configer{///<summary>//    Website Information configuration node///    </summary> public    class webconfigsection:configurationsection {//<summary>///Website name///        </summary>        [ ConfigurationProperty ("WebName", DefaultValue = "", IsRequired = True, IsKey = False)] public        string WebName        {
   get {return (string) this["WebName"];}            set {this["WebName"] = value;}        }        <summary>        ////Website domain///</summary> [ConfigurationProperty ("domain", defaultvalue = "", IsRequired = True, IsKey = False)] public        string DoMain        {            get {return (string) this["DoMain"];}            set {this["DoMain"] = value;}}}    

Entity factory class design, mainly used to produce entity configuration information

namespace configer{///<summary>///    website configuration information Factory///    </summary> public    class Webconfigmanager {//<summary>///        configuration information entities///</summary> public        static readonly Webconfigsection Instance = GetSection ();        private static Webconfigsection GetSection ()        {            webconfigsection config = configurationmanager.getsection (" Webconfigsection ") as Webconfigsection;            if (config = = null)                throw new Configurationerrorsexception ();            return config;}}    }

And finally, the. config file, which has configsections and the specified sections block, it is important to note that the configsections must be in the first position of the configuration

<?xml version= "1.0" encoding= "Utf-8"?><configuration>  <configSections>    <section Name = "Webconfigsection" type= "configer.webconfigsection, test"/>  </configSections>  < connectionstrings>    <add name= "backgroundentities" connectionstring= "metadata=res://*/model1.csdl|res:/ /*/model1.ssdl|res://*/model1.msl;provider=system.data.sqlclient;provider Connection String=&quot;data Source =.\sqlexpress;initial catalog=background;integrated security=true; multipleactiveresultsets=true&quot; "providername=" System.Data.EntityClient "/>  </ connectionstrings>  <webconfigsection webname= "account Site" domain= "www.zhanzhan.com"  />  < appsettings>    <add key= "Site" value= "www.zzl.com"/>  </appSettings></configuration>

After the implementation of the above three steps, we can call it, hehe

  static void Main (string[] args)   {     Console.WriteLine (System.Configuration.ConfigurationManager.AppSettings ["Site"]);     Console.WriteLine (WebConfigManager.Instance.DoMain);     Console.WriteLine (WebConfigManager.Instance.WebName);   }

The results are as follows:

. NET how to customize config configuration file nodes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.