简体   繁体   中英

How to read nested loop json values from appsettings.json and assign to object

"SalesOrderFilters": {
    "skip": 0,
    "take": 0,
    "sort": [
      {
        "sortBy": "productGroup",
        "sortDirection": "Ascending"
      }
    ],
    "filters": [
      {
        "columnFilters": [
          {
            "field": "productGroup",
            "operator": "eq",
            "value": "CC05"
          },
          {
            "field": "productGroup",
            "operator": "eq",
            "value": "CC07"
          }
        ],
        "logic": "or"
      },
      {
        "columnFilters": [
          {
            "field": "specFileDate",
            "operator": "lte",
            "value": "6/26/2017 11:17:20 AM"
          },
          {
            "field": "specFileDate",
            "operator": "gte",
            "value": "4/26/2017 11:17:20 AM"
          }
        ],
        "logic": "and"
      },
      {
        "columnFilters": [
          {
            "field": "designSpecialIndicator",
            "operator": "eq",
            "value": "N"
          }
        ],
        "logic": "and"
      },
      {
        "columnFilters": [
          {
            "field": "buMfgLocId",
            "operator": "eq",
            "value": "5"
          }
        ],
        "logic": "and"
      },
      {
        "columnFilters": [
          {
            "field": "orderType",
            "operator": "eq",
            "value": "SO"
          }
        ],
        "logic": "and"
      },
      {
        "columnFilters": [
          {
            "field": "buMfgLocation",
            "operator": "contains",
            "value": "lexi"
          }
        ],
        "logic": "and"
      }
    ]
  }

You should look at the options pattern .

The options pattern uses classes to provide strongly typed access to groups of related settings. When configuration settings are isolated by scenario into separate classes, the app adheres to two important software engineering principles:

The Interface Segregation Principle (ISP) or Encapsulation: Scenarios (classes) that depend on configuration settings depend only on the configuration settings that they use. Separation of Concerns: Settings for different parts of the app aren't dependent or coupled to one another.

Just want to share how I do it on my apps (assuming appsettings.json is your default config file):

public class ConfigHelper : IConfigHelper
{
    private IConfiguration Configuration { get; }

    public ConfigHelper(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public string GetAppSettings(string key)
    {
        return Configuration[key];
    }
}

Usage:

...
ConfigHelper.GetAppSettings("SalesOrderFilters:Skip");
...

Or this approach:

create a class to represent your config:

public class SalesOrderFilters
{
    public int Skip { get; set; }
    public int Take { get; set; }
    public List<Filters> Filters { get; set; }
    ...
}

public class Filter
{
   public List<ColumnFilter> ColumnFilters { get; set; }
   ...
}

in your Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddSingleton(typeof(SalesOrderFilters), Configuration.GetSection("SalesOrderFilters").Get<SalesOrderFilters>());
    ...
}

And then you can inject this object in your controllers/service classes:

public class MyController
{
    private SalesOrderFilters _settings;

    public MyController(SalesOrderFilters settings)
    {
        _settings = settings;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM