来自 IConfigurationSection 集合的 AutoMapper 映射 [英] AutoMapper map from collection of IConfigurationSection
本文介绍了来自 IConfigurationSection 集合的 AutoMapper 映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经浏览了关于映射集合和嵌套映射以及嵌套集合映射的文档,但仍然无法处理我的情况.
我有以下 json 配置文件:
I've browsed documentation on mapping collections and nested mapping and mapping of nested collection, but still can't cope with my case.
I have the following json config file:
{
"startupConfig": {
"noSubscription": {
"calls": [
{
"percentage": 30,
"techPriority": 1,
"timePriority": 2
},
{
"percentage": 30,
"techPriority": 1,
"timePriority": 2
}
]
}
}
}
这是我从文件中读取的代码:
And here is my code reading from the file:
var config = _mapper.Map<FeedConfiguration>(_configuration
.GetSection("startupConfig").GetChildren()
.FirstOrDefault(cc => cc.Key == "noSubscription")?.GetChildren());
但是,映射不起作用.下面是映射配置的代码:
However, the mapping does not work. Here is the code of mapping configuration:
CreateMap<IEnumerable<IConfigurationSection>, CallConfiguration>()
.ForMember(cc => cc.Percentage,
mo => mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "percentage").Value))
.ForMember(cc => cc.TechPriority,
mo => mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "techPriority").Value))
.ForMember(cc => cc.TimePriority,
mo => mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "timePriority").Value));
CreateMap<IEnumerable<IConfigurationSection>, FeedConfiguration>()
.ForMember(fc => fc.CallsConfig,
mo => mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "calls").GetChildren()));
我映射到的类:
namespace FeedService.FeedConfigurations
{
public class FeedConfiguration
{
public ICollection<CallConfiguration> CallsConfig { get; set; }
}
public class CallConfiguration
{
public int Percentage { get; set; }
public int TechPriority { get; set; }
public int TimePriority { get; set; }
}
}
这是我得到的一个例外:
And here is an exception I get:
AutoMapper.AutoMapperConfigurationException:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
=============================================================================================================
AutoMapper created this type map for you, but your types cannot be mapped using the current configuration.
IConfigurationSection -> CallConfiguration (Destination member list)
Microsoft.Extensions.Configuration.IConfigurationSection -> FeedService.FeedConfigurations.CallConfiguration (Destination member list)
Unmapped properties:
Percentage
TechPriority
TimePriority
非常感谢您的帮助!
===注意====
我仍然需要这个问题的答案,但我发布了一个更好的解释的新答案 此处.
推荐答案
这里实际上并不需要 Automepper.只需使用默认的 .net 核心绑定.
You don't actually need Automepper here. Just use default .net core binding.
重命名这个
public ICollection<CallConfiguration> CallsConfig { get; set; }
到调用
然后使用类似的东西
var config = _configuration.GetSection("startupConfig:noSubscription").Get<FeedConfiguration>();
这篇关于来自 IConfigurationSection 集合的 AutoMapper 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文