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

AutoMapper map from collection of IConfigurationSection with complex mapping

这是我关于这个话题的第二个问题。我的第一个问题是 。 Roman Marusyk 为这个问题提供了一个简单的答案。然而,我在现实中有更困难的案例,它会变得更加复杂。因此,我需要使用 AutoMapper 来映射配置(尽管如果默认绑定也能解决这个问题,我会感到非常高兴和惊讶)。
这是我的真实 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 的配置值映射到 属性,并按名称使用枚举类型。显然,默认配置活页夹对我没有任何作用。我也不想将 属性 的类型更改为字符串或将值的类型更改为整数。因此,我仍然需要使用 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; }
    }
}

我得到一个例外:

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