C # software configuration,

Source: Internet
Author: User

C # software configuration,

The configuration function is necessary for the software. The following describes the built-in configuration framework of Glacier. Glacier supports three configuration methods:

  • AppSettings Configuration
  • Embedded HOCON Configuration
  • Independent HOCON Configuration

Priority: Independent HOCON configuration> embedded HOCON configuration> receivettings Configuration

The configuration with a higher priority will overwrite the configuration with a lower priority.

For Glacier frame usage, visit: https://www.gkarch.com/

1 appSetting Configuration

For example, the following configuration exists in deleetting:

<configuration>  <appSettings>    <add key="prefix:key1" value="42" />    <add key="prefix:key2" value="foo,bar" />    <add key="prefix:key3:innerKey" value="hello world" />  </appSettings></configuration>

Through the Glacier framework, you can obtain the configuration by using the following code (you can directly convert the configuration to a specific type through)

// When the program starts, add the configuration prefix to be loaded: GlacierSystem. core. addAppSettingsConfig ("prefix"); // when you need to obtain the configuration: var config = GlacierSystem. core. getConfig ("prefix"); var val1 = config ["key1"]. asInt (); var val2 = config ["key2"]. asList <string> (); var innerVal = config ["key3: innerKey"]. asString (); // or var innerVal = config. getSub ("key3") ["innerKey"]. asString ();
2 embedded HOCON Configuration

HOCON-based configuration supports the type binding function. You can directly bind the configuration to a specific class to make the configuration more readable and easy to use and manage.

We still use the previous example to use the embedded HOCON configuration.

<configuration>  <configSections>    <section name="glacier"             type="GKarch.Glacier.Configuration.HoconSection, GKarch.Glacier" />  </configSections>  <glacier>    <![CDATA[prefix {  key1 = 42  key2 = [foo, bar]  key3 = {    innerKey = "hello world"  }}]]>  </glacier></configuration>

Define a class to correspond to this Configuration:

// Custom model class MyConfig {public int Key1 {get; set;} public IList <string> Key2 {get; set;} public IDictionary <string, string> Key3 {get; set ;}}

Read the configuration and bind it to the model

// Obtain the configuration and bind it to the custom Model
MyConfig config = GlacierSystem.Core.GetConfig("prefix").Bind<MyConfig>;
3 independent HOCON Configuration

The HOCON configuration can be an independent configuration file, and the switch between the development and production environments is more convenient through an independent configuration file,

First, define the configuration file in appSetting.

<Configuration> <appSettings> <add key = "glacier: config: provider" value = "hocon"/> <! -- The config. conf file is loaded by default. You can add the following configuration to other file names --> <! -- <Add key = "glacier: config: hocon: file" value = "my. conf"/> --> </appSettings> </configuration>

Config. conf configuration file content

prefix {  key1 = 42  key2 = [foo, bar]  key3 = {    innerKey = "hello world"  }}

This time, a nested type is used to correspond to the configuration. The following two classes are defined to correspond to the configuration.

class MyConfig{    public int Key1 { get; set; }    public IList<string> Key2 { get; set; }    public MyInnerConfig Key3 { get; set; }}class MyInnerConfig{    public string InnerKey { get; set; }}

Read Configuration

var config = GlacierSystem.Core.GetConfig("prefix").Bind<MyConfig>();Console.WriteLine(config.Key3.InnerKey);  // hello world

Related Article

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.