简体   繁体   English

如何从 appsettings.json 读取嵌套循环 json 值并分配给对象

[英]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.接口隔离原则 (ISP) 或封装:依赖于配置设置的场景(类)仅依赖于它们使用的配置设置。 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):只想分享我如何在我的应用程序上执行此操作(假设 appsettings.json 是您的默认配置文件):

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在您的 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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