来自IConfigurationSection集合的AutoMapper映射与复杂映射

时间:2019-06-06 11:23:26

标签: c# .net-core automapper

这是我关于这个问题的第二个问题。我的第一个问题是here。 罗马·马鲁西克(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

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您的个人资料必须看起来像这样

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(附加选项)

CreateMap<IConfigurationSection, FeedConfiguration>()
                .ForMember(fc => fc.Calls,
                    mo => mo.MapFrom(fc => fc.GetChildren().FirstOrDefault(fd=>fd.Key == "calls").GetChildren()));