.NET Core combat development (Lesson 13: bonding configuration: Use strongly typed objects bearer configuration data) - Study Notes

13 | configuration binding: bearer configuration data strongly typed objects

Highlights:

1, configuration values ​​will be bound to support existing object

2, will be bound to support the configuration values ​​on private property

Continue to use the code on a

Receiving a first defined as a configuration example of the class

class Config
{
    public string Key1 { get; set; }
    public bool Key5 { get; set; }
    public int Key6 { get;  set; }
}

Then look at the configuration file, appsettings.json

{
  "Key1": "Value1",
  "Key2": "Value2",
  "Key5": true,
  "Key6": 0
}

Add a reference to the package

  • Microsoft.Extensions.Configuration.Binder

The role of the package is to allow us to easily configure the binding to the strongly typed to the top

The main program

var builder = new ConfigurationBuilder();
builder.AddJsonFile("appsettings.json", optional:true, reloadOnChange:true);
var configurationRoot = builder.Build();

var config = new Config()
{
    Key1 = "config key1",
    Key5 = false,
    Key6 = 100
};

configurationRoot.Bind(config);

Console.WriteLine($"Key1:{config.Key1}");
Console.WriteLine($"Key5:{config.Key5}");
Console.WriteLine($"Key6:{config.Key6}");

Start the program, the output is as follows:

Key1:Value1
Key5:True
Key6:0

As can be seen, the field is read out of the binding from the configuration

In fact, generally speaking, the profile would not be so simple, usually nested format

{
  "Key2": "Value2",
  "Key6": 0,
  "OrderService": {
    "Key1": "order key1",
    "Key5": true,
    "Key6": 200
  }
}

In this case, we need to bind to the config section objects

configurationRoot.GetSection("OrderService").Bind(config);

This may be grouped in different configurations, and are respectively bound, mixed avoidance

Start the program, the output is as follows:

Key1:order key1
Key5:True
Key6:200

That configuration can be read from an arbitrary section, and bound to the above types

All types defined here, all fields are public, but there are some scenes the following may be private, for private fields, by default, is not going to bind, but also does not allow assign default values ​​can be defined set up

class Config
{
    public string Key1 { get; set; }
    public bool Key5 { get; set; }
    public int Key6 { get; private set; } = 100;
}

The main program

var config = new Config()
{
    Key1 = "config key1",
    Key5 = false
};

configurationRoot.GetSection("OrderService").Bind(config);

Start the program, the output is as follows:

Key1:order key1
Key5:True
Key6:100

Key6 can see the value is 100, does not change, the value 200 is disposed

Let the private variables to take effect, in fact, there is another argument Bind

configurationRoot.GetSection("OrderService").Bind(config,
                binderOptions => { binderOptions.BindNonPublicProperties = true; });

Start the program, the output is as follows:

Key1:order key1
Key5:True
Key6:200

As a result, private field can also be assigned from the configuration inside

Creative Commons License

This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing.

Welcome to reprint, use, repost, but be sure to keep the article signed by Zheng Ziming (containing links: http://www.cnblogs.com/MingsonZheng/), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification .

If you have any questions, please contact me ([email protected]).

Guess you like

Origin www.cnblogs.com/MingsonZheng/p/12387527.html