[ASP. 5] configuration-New generation of configuration files (multiple implementations of Configurationsource)

Source: Internet
Author: User

About the configuration file directory: [ASP. 5] configuration-Next-generation configuration file

In the previous we introduced, the system uses the Iconfigurationsource to represent the different configuration file source, plays the function which reads, sets, loads the configuration file. The virtual class Configurationsource inherits the interface Iconfigurationsource, and the other classes are derived from Configurationsource (of course we can also write inherited from the interface Iconfigurationsource class, But there is no need.) The following are the projects that implement different configuration methods:

Here we mainly use test cases to explain the JSON and XML configuration file source code and practical way:

Microsoft.Framework.Configuration.Json

JSON configuration file: Using Newtonsoft.json 's NuGet package during implementation, this is a very well-known JSON operations component, which is recommended if you have a separate JSON operation involving. NET.

    • JSON contains the use of arrays:
 Public voidarrayofobjects () {varJSON =@"{' IP ': [{' Address ': ' 1.2.3.4 ', ' hid                        Den ': false}, {' Address ': ' 5.6.7.8 ', ' Hidden ': true}]}"; varJsonconfigsource =NewJsonconfigurationsource (Teststreamhelpers.arbitraryfilepath);            Jsonconfigsource.load (Teststreamhelpers.stringtostream (JSON)); Assert.equal ("1.2.3.4", Jsonconfigsource.get ("ip:0:address")); Assert.equal ("False", Jsonconfigsource.get ("Ip:0:hidden")); Assert.equal ("5.6.7.8", Jsonconfigsource.get ("ip:1:address")); Assert.equal ("True", Jsonconfigsource.get ("Ip:1:hidden")); }
arrayofobjects
    • Use of JSON multi-configuration files:
 Public voidexplicitarrayreplacement () {varJson1 =@"{' IP ': [' 1.2.3.4 ', ' 7.8.9.10 ', ' 11.12.13.14 ' ]            }"; varJson2 =@"{' IP ': {' 1 ': ' 15.16.17.18 '}}"; varJsonConfigSource1 =NewJsonconfigurationsource (Teststreamhelpers.arbitraryfilepath);            Jsonconfigsource1.load (Teststreamhelpers.stringtostream (Json1)); varJsonConfigSource2 =NewJsonconfigurationsource (Teststreamhelpers.arbitraryfilepath);            Jsonconfigsource2.load (Teststreamhelpers.stringtostream (Json2)); varBuilder =NewConfigurationbuilder (); Builder. ADD (JsonConfigSource1, load:false); Builder. ADD (jsonConfigSource2, load:false); varConfig =Builder.            Build (); Assert.equal (3, CONFIG. Getconfigurationsections ("IP").            Count ()); Assert.equal ("1.2.3.4", CONFIG. Get ("ip:0")); Assert.equal ("15.16.17.18", CONFIG. Get ("ip:1")); Assert.equal ("11.12.13.14", CONFIG. Get ("Ip:2")); }
explicitarrayreplacement
    • The order in which the configuration files are sorted (the key value is sorted when all child keys are obtained)

 Public voidPropertiesaresortedbynumberonlyfirst () {varJSON =@"{' setting ': {' hello ': ' A ', ' Bob ': ' B ', ' 42 ':            ' C ', ' 4 ': ' d ', ' ten ': ' E ', ' 1text ': ' F ',} }"; varJsonconfigsource =NewJsonconfigurationsource (Teststreamhelpers.arbitraryfilepath);            Jsonconfigsource.load (Teststreamhelpers.stringtostream (JSON)); varBuilder =NewConfigurationbuilder (); Builder. ADD (Jsonconfigsource, load:false); varConfig =Builder.            Build (); varConfigurationSection = config. Getconfigurationsection ("setting"); varIndexconfigurationsections =configurationsection.getconfigurationsections ().            ToArray (); Assert.equal (6, Indexconfigurationsections.count ()); Assert.equal ("4", indexconfigurationsections[0].            Key); Assert.equal ("Ten", indexconfigurationsections[1].            Key); Assert.equal (" the", indexconfigurationsections[2].            Key); Assert.equal ("1text", indexconfigurationsections[3].            Key); Assert.equal ("Bob", indexconfigurationsections[4].            Key); Assert.equal ("Hello", indexconfigurationsections[5].        Key); }
Propertiesaresortedbynumberonlyfirst

Microsoft.Framework.Configuration.Xml

XML configuration file, in daily uses is also more, the traditional configuration file is XML. [ the implementation of the service is to support the content encryption, specifically do not understand, slightly ]

The following is the general use of XML files:

 Public voidsupportandignorexmldeclaration () {varXML =@"<?xml version= ' 1.0 ' encoding= ' UTF-8 '?> <settings> <Data> <DefaultConnection> <connectionstring>testconnectionstring</connec Tionstring> <Provider>SqlClient</Provider> </defaultco Nnection> <Inventory> <connectionstring>anothertestconnec                        Tionstring</connectionstring> <Provider>MySql</Provider> </Inventory> </Data> </settings>"; varXMLCONFIGSRC =NewXmlconfigurationsource (Arbitraryfilepath);            Xmlconfigsrc.load (Teststreamhelpers.stringtostream (XML)); Assert.equal ("testconnectionstring", Xmlconfigsrc.get ("Data:DefaultConnection:ConnectionString")); Assert.equal ("SqlClient", Xmlconfigsrc.get ("Data:DefaultConnection:Provider")); Assert.equal ("anothertestconnectionstring", Xmlconfigsrc.get ("Data:Inventory:ConnectionString")); Assert.equal ("MYSQL", Xmlconfigsrc.get ("Data:Inventory:Provider")); }
Xmlconfigurationsource

[ASP. 5] configuration-New generation of configuration files (multiple implementations of Configurationsource)

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.