gpt4 book ai didi

c# - 具有复杂映射的 IConfigurationSection 集合中的 AutoMapper 映射

转载 作者:太空宇宙 更新时间:2023-11-03 12:02:30 25 4
gpt4 key购买 nike

这是我关于该主题的第二个问题。我的第一个问题是 here .Roman Marusyk 为这个问题提供了一个简单的答案。然而,我在现实中有更困难的案例,它会变得更加复杂。因此,我需要使用 AutoMapper 来映射配置(尽管如果默认绑定(bind)也能解决这个问题,我会感到非常高兴和惊讶)。
这是我的真实 json,后面跟着模型:

{
"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; }
}
}

在这里,如您所见,我需要将 profiles:type 的配置值按名称映射到具有枚举类型的属性。显然,默认配置 Binder 对我没有任何作用。我也不想将属性的类型更改为字符串或将值的类型更改为整数。因此,对于使用 AutoMapper 进行映射的原始问题,我仍然需要一个答案(其中缩短的示例足以将其扩展到第二部分)。
===为方便起见复制了原始问题===
我有以下 json 配置文件:

{
"startupConfig": {
"noSubscription": {
"calls": [
{
"percentage": 30,
"techPriority": 1,
"timePriority": 2
},
{
"percentage": 30,
"techPriority": 1,
"timePriority": 2
}
]
}
}
}

这是我从文件中读取的代码:

var config = _mapper.Map<FeedConfiguration>(_configuration
.GetSection("startupConfig").GetChildren()
.FirstOrDefault(cc => cc.Key == "noSubscription")?.GetChildren());

但是,映射不起作用。下面是映射配置的代码:

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; }
}
}

这是我得到的一个异常(exception):

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

非常感谢您的帮助!

最佳答案

您的个人资料必须如下所示

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));
}

}
}

然后使用映射器

_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()));

关于c# - 具有复杂映射的 IConfigurationSection 集合中的 AutoMapper 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56476564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com