The foundation is the most serious-use good configsections to make configuration information more standardized

Source: Internet
Author: User

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:

namespaceconfiger{/// <summary>    ///Site information Configuration node/// </summary>     Public classwebconfigsection:configurationsection {/// <summary>        ///Site name/// </summary>[ConfigurationProperty ("WebName", DefaultValue ="", isrequired =true, IsKey =false)]         Public stringWebName {Get{return(string) This["WebName"]; }            Set{ This["WebName"] =value;} }        /// <summary>        ///website domain name/// </summary>[ConfigurationProperty ("DoMain", DefaultValue ="", isrequired =true, IsKey =false)]         Public stringDoMain {Get{return(string) This["DoMain"]; }            Set{ This["DoMain"] =value;} }    }}

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

namespaceconfiger{/// <summary>    ///Site configuration Information Factory/// </summary>     Public classWebconfigmanager {/// <summary>        ///Configuration Information Entity/// </summary>         Public Static ReadOnlyWebconfigsection Instance =GetSection (); Private Staticwebconfigsection GetSection () {webconfigsection config= Configurationmanager.getsection ("webconfigsection") aswebconfigsection; if(config = =NULL)                Throw Newconfigurationerrorsexception (); returnconfig; }    }}

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="Occupied 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:

The foundation is the most serious-use good configsections to make configuration information more standardized

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.