Search code examples
bit-framework

How to have nested configurations in config json file and read it using bit?


so far I've seen some sample code in which some configuration is inserted in a file named environment.json like this:

[
    {
        "Name": "Default",

    "AppInfo": {
          "Name": "blahblah",
          "Version": "1"
    },
    "Configs": [
      {
        "Key": "someconfiguration",
        "Value": "some value"
      },
      {
        "Key": "another configuration ",
        "Value": "blah blah"
      },
    ]
   }
]

and then when needed, data can be read from configuration file like this:

var value = DefaultAppEnvironmentsProvider.Current
            .GetActiveAppEnvironment()
            .GetConfig<string>("SomeConfiguration");

The Question is:

what if I want to have some configuration whose value is a nested json list or json object. I want some thing like this:

"key": "Address",
"value": {
    "street": "some street name",
    "postal code": "blah blah",
    ...
}

how can I read such configurations using bit?

thanks for your time in advance.


Solution

  • First of all, create a class which defines your configuration contract:

    public class MailServerConfig
    {
        public Uri ServerUrl { get; set; }
    
        public string AnotherConfig { get; set; }
    }
    

    Then add followings to your environments.json file:

    ,
          {
            "Key": "MailServerConfig",
            "Value": {
              "$type": "SampleApp.Configurations.MailServerConfig, SampleApp",
              "ServerUrl": "https://google.com/",
              "AnotherConfig": "!"
            }
          }
    

    In your controllers (Or wherever you want to read your configs), inject AppEnv

    public AppEnvironment AppEnv { get; set; }
    

    Then read your config as below:

    MailServerConfig mailServerConfig = AppEnv.GetConfig<MailServerConfig>("MailServerConfig");
    

    Note that in environments.json, $type is mandatory, and it should be the first line of your nested json object.