来自IConfigurationSection集合的AutoMapper映射与复杂映射 [英] AutoMapper map from collection of IConfigurationSection with complex mapping

查看:103
本文介绍了来自IConfigurationSection集合的AutoMapper映射与复杂映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我关于这个问题的第二个问题.我的第一个问题是此处. 罗马·马鲁西克(Roman Marusyk)为这个问题提供了一个简单的答案.但是,现实中我遇到的困难更大,情况将变得更加复杂.因此,我需要使用AutoMapper来映射配置(尽管如果默认绑定也可以解决这个问题,我会感到非常高兴和惊讶).
这是我的真实json,后面是模型:

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

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

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

我要映射的类:

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

非常感谢您的帮助!

推荐答案

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

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

    }
}

然后使用映射器

_mapper.Map<FeedConfiguration>(_configuration.GetSection("startupConfig:noSubscription"));

EDIT(附加选项)

EDIT(additional option)

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

这篇关于来自IConfigurationSection集合的AutoMapper映射与复杂映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆