AutoMapper 从具有复杂映射的 IConfigurationSection 集合中映射 [英] AutoMapper map from collection of IConfigurationSection with complex mapping
问题描述
这是我关于该主题的第二个问题.我的第一个问题是这里.Roman Marusyk 为这个问题提供了一个简单的答案.但是,我在现实中遇到了更困难的情况,并且会变得更加复杂.因此,我需要使用 AutoMapper 来映射配置(尽管如果默认绑定也能解决这个问题,我会非常高兴和惊讶).
这是我的真实 json 模型,后面跟着:
<代码>{启动配置":{无订阅":{电话":[{百分比":30,技术优先级":1,时间优先级":2},{百分比":30,技术优先级":1,时间优先级":2}],个人资料":[{类型":启动",百分比":20,技术优先级":2,时间优先级":1}]}}}
命名空间 FeedService.FeedConfigurations{公共类 FeedConfiguration{公共 ICollectionCallsConfig { 获取;放;}公共 ICollectionProfilesConfig { 获取;放;}}公共类 ProfileConfiguration{公共 CompanyTypeEnum CompanyTypeEnum { 获取;放;}公共整数百分比{得到;放;}公共 int TechPriority { 得到;放;}公共 int TimePriority { 得到;放;}}公共类 CallConfiguration{公共整数百分比{得到;放;}公共 int TechPriority { 得到;放;}公共 int TimePriority { 得到;放;}}}
在这里,如您所见,我需要将 profiles:type
的配置值映射到具有枚举类型的属性.显然,默认配置活页夹对我没有魔法.我也不想将属性的类型更改为字符串或将值的类型更改为整数.因此,对于使用 AutoMapper 进行映射的原始问题,我仍然需要一个答案(其中缩短的示例足以将其扩展到第二部分).
===为方便起见复制了原始问题===
我有以下 json 配置文件:
<代码>{启动配置":{无订阅":{电话":[{百分比":30,技术优先级":1,时间优先级":2},{百分比":30,技术优先级":1,时间优先级":2}]}}}
这是我从文件中读取的代码:
var config = _mapper.Map(_configuration.GetSection("startupConfig").GetChildren().FirstOrDefault(cc => cc.Key == "noSubscription")?.GetChildren());
但是,映射不起作用.下面是映射配置的代码:
CreateMap, CallConfiguration>().ForMember(cc => cc.Percentage,莫 =>mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "percentage").Value)).ForMember(cc => cc.TechPriority,莫 =>mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "techPriority").Value)).ForMember(cc => cc.TimePriority,莫 =>mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "timePriority").Value));CreateMap、FeedConfiguration>().ForMember(fc => fc.CallsConfig,莫 =>mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "calls").GetChildren()));
我映射到的类:
命名空间 FeedService.FeedConfigurations{公共类 FeedConfiguration{公共 ICollectionCallsConfig { 获取;放;}}公共类 CallConfiguration{公共整数百分比{得到;放;}公共 int TechPriority { 得到;放;}公共 int TimePriority { 得到;放;}}}
这是我得到的一个例外:
AutoMapper.AutoMapperConfigurationException:找到了未映射的成员.查看下面的类型和成员.添加自定义映射表达式、忽略、添加自定义解析器或修改源/目标类型对于没有匹配的构造函数,添加一个无参数构造函数,添加可选参数,或映射所有构造函数参数==============================================================================================================AutoMapper 为您创建了此类型映射,但您的类型无法使用当前配置进行映射.IConfigurationSection ->CallConfiguration(目标成员列表)Microsoft.Extensions.Configuration.IConfigurationSection ->FeedService.FeedConfigurations.CallConfiguration(目标成员列表)未映射的属性:百分比技术优先时间优先级
非常感谢您的帮助!
您的个人资料必须是这样的
命名空间 FeedService.FeedConfigurations{公共类 FeedConfigurationMappingProfile :配置文件{公共 FeedConfigurationMappingProfile(){CreateMap().ForMember(fc => fc.Calls,莫 =>mo.MapFrom(fc => fc.Get().Calls));CreateMap().ForMember(cc => cc.Percentage,莫 =>mo.MapFrom(css => css.GetChildren().FirstOrDefault(cs => cs.Key == "percentage").Value)).ForMember(cc => cc.TechPriority,莫 =>mo.MapFrom(css => css.GetChildren().FirstOrDefault(cs => cs.Key == "techPriority").Value)).ForMember(cc => cc.TimePriority,莫 =>mo.MapFrom(css => css.GetChildren().FirstOrDefault(cs => cs.Key == "timePriority").Value));}}}
然后使用映射器
_mapper.Map(_configuration.GetSection("startupConfig:noSubscription"));
<块引用>
编辑(附加选项)
CreateMap().ForMember(fc => fc.Calls,莫 =>mo.MapFrom(fc => fc.GetChildren().FirstOrDefault(fd=>fd.Key == "calls").GetChildren()));
It is my second question on the topic. My first question is here.
Roman Marusyk provided an easy answer to the question. However, I have more difficult case in reality and it will get even more complex. Therefore, I need to use AutoMapper to map the config (though I would be very happy and surprised if default binding coped with this as well).
Here is my real json with models followed:
{
"startupConfig": {
"noSubscription": {
"calls": [
{
"percentage": 30,
"techPriority": 1,
"timePriority": 2
},
{
"percentage": 30,
"techPriority": 1,
"timePriority": 2
}
],
"profiles": [
{
"type": "startup",
"percentage": 20,
"techPriority": 2,
"timePriority": 1
}
]
}
}
}
namespace FeedService.FeedConfigurations
{
public class FeedConfiguration
{
public ICollection<CallConfiguration> CallsConfig { get; set; }
public ICollection<ProfileConfiguration> ProfilesConfig { get; set; }
}
public class ProfileConfiguration
{
public CompanyTypeEnum CompanyTypeEnum { get; set; }
public int Percentage { get; set; }
public int TechPriority { get; set; }
public int TimePriority { get; set; }
}
public class CallConfiguration
{
public int Percentage { get; set; }
public int TechPriority { get; set; }
public int TimePriority { get; set; }
}
}
Here, as you see I need config value of profiles:type
to be mapped to property with enum type by name. Apparently, default configuration binder does not do magic for me. I also would not like to change type of the property to string or type of the value to integer. Therefore, I still need an answer for the original question for mapping with AutoMapper (where shortened example is enough to extend it for the second part).
===Original question copied for convenience===
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()));
Classes that I am mapping to:
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
Would be very thankful for your help!
Your profile must look like this
namespace FeedService.FeedConfigurations
{
public class FeedConfigurationMappingProfile : Profile
{
public FeedConfigurationMappingProfile()
{
CreateMap<IConfigurationSection, FeedConfiguration>()
.ForMember(fc => fc.Calls,
mo => mo.MapFrom(fc => fc.Get<FeedConfiguration>().Calls));
CreateMap<IConfigurationSection, CallConfiguration>()
.ForMember(cc => cc.Percentage,
mo => mo.MapFrom(css => css.GetChildren().FirstOrDefault(cs => cs.Key == "percentage").Value))
.ForMember(cc => cc.TechPriority,
mo => mo.MapFrom(css => css.GetChildren().FirstOrDefault(cs => cs.Key == "techPriority").Value))
.ForMember(cc => cc.TimePriority,
mo => mo.MapFrom(css => css.GetChildren().FirstOrDefault(cs => cs.Key == "timePriority").Value));
}
}
}
and then use mapper
_mapper.Map<FeedConfiguration>(_configuration.GetSection("startupConfig:noSubscription"));
EDIT(additional option)
CreateMap<IConfigurationSection, FeedConfiguration>()
.ForMember(fc => fc.Calls,
mo => mo.MapFrom(fc => fc.GetChildren().FirstOrDefault(fd=>fd.Key == "calls").GetChildren()));
这篇关于AutoMapper 从具有复杂映射的 IConfigurationSection 集合中映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!