如何将选项中的子数组映射到字典中
How to map a sub-array in Options into a Dictionary
我在 appsettings.json
中的选项如下所示:
"ApplicationSettings": {
"Value1": "abc",
"Value2": 5,
...
"MaxDOP": [
{
"Region": "A",
"ThreadCount": 20
},
{
"Region": "B",
"ThreadCount": 30
},
...
]
}
我照常注射:
services.Configure<Models.AppConfigModel>(Configuration.GetSection("ApplicationSettings"));
我的问题是如何映射 MaxDOP
数组,以便它将通过 IOptionsMonitor<>
保持最新的同步。 一般处理数组,但在我的例子中 Region
属性是唯一的,我宁愿使用 Dictionary<string, int>
(或者可能 Dictionary<string, MaxDopSettings>
以防万一那里有多个非关键值),以便我可以有效地按区域代码进行搜索。
能做到吗?
解决方案 1:映射字典
将 MaxDOP
的签名从列表更改为字典
"MaxDOP": {
"A": {
"ThreadCount": 20
},
"B": {
"ThreadCount": 30
}
}
打class:
public class Dop
{
public int ThreadCount { get; set; }
}
正在配置:
services.Configure<Dictionary<string, Dop>>(Configuration.GetSection("ApplicationSettings:MaxDOP"));
用法:
public HomeController(IOptionsMonitor<Dictionary<string, Dop>> monitor)
{
_monitor = monitor;
Dictionary<string, Dop> a = _monitor.CurrentValue;
}
解决方案 2:将字典包含到主设置 POCO
将 MaxDOP
的签名从列表更改为字典:
appsettings.json:
"ApplicationSettings": {
"Value1": "abc",
"Value2": 5,
"MaxDOP": {
"A": {
"ThreadCount": 20
},
"B": {
"ThreadCount": 30
}
}
}
Poco classes:
public class ApplicationSettings
{
public string Value1 { get; set; }
public int Value2 { get; set; }
public Dictionary<string, Dop> MaxDOP { get; set; }
}
public class Dop
{
public int ThreadCount { get; set; }
}
正在配置:
services.Configure<ApplicationSettings>(Configuration.GetSection("ApplicationSettings"));
用法:
public HomeController(IOptionsMonitor<ApplicationSettings> monitor)
{
_monitor = monitor;
ApplicationSettings a = _monitor.CurrentValue;
}
我在 appsettings.json
中的选项如下所示:
"ApplicationSettings": {
"Value1": "abc",
"Value2": 5,
...
"MaxDOP": [
{
"Region": "A",
"ThreadCount": 20
},
{
"Region": "B",
"ThreadCount": 30
},
...
]
}
我照常注射:
services.Configure<Models.AppConfigModel>(Configuration.GetSection("ApplicationSettings"));
我的问题是如何映射 MaxDOP
数组,以便它将通过 IOptionsMonitor<>
保持最新的同步。 Region
属性是唯一的,我宁愿使用 Dictionary<string, int>
(或者可能 Dictionary<string, MaxDopSettings>
以防万一那里有多个非关键值),以便我可以有效地按区域代码进行搜索。
能做到吗?
解决方案 1:映射字典
将 MaxDOP
的签名从列表更改为字典
"MaxDOP": {
"A": {
"ThreadCount": 20
},
"B": {
"ThreadCount": 30
}
}
打class:
public class Dop
{
public int ThreadCount { get; set; }
}
正在配置:
services.Configure<Dictionary<string, Dop>>(Configuration.GetSection("ApplicationSettings:MaxDOP"));
用法:
public HomeController(IOptionsMonitor<Dictionary<string, Dop>> monitor)
{
_monitor = monitor;
Dictionary<string, Dop> a = _monitor.CurrentValue;
}
解决方案 2:将字典包含到主设置 POCO
将 MaxDOP
的签名从列表更改为字典:
appsettings.json:
"ApplicationSettings": {
"Value1": "abc",
"Value2": 5,
"MaxDOP": {
"A": {
"ThreadCount": 20
},
"B": {
"ThreadCount": 30
}
}
}
Poco classes:
public class ApplicationSettings
{
public string Value1 { get; set; }
public int Value2 { get; set; }
public Dictionary<string, Dop> MaxDOP { get; set; }
}
public class Dop
{
public int ThreadCount { get; set; }
}
正在配置:
services.Configure<ApplicationSettings>(Configuration.GetSection("ApplicationSettings"));
用法:
public HomeController(IOptionsMonitor<ApplicationSettings> monitor)
{
_monitor = monitor;
ApplicationSettings a = _monitor.CurrentValue;
}