来自IConfigurationSection集合的AutoMapper映射 [英] AutoMapper map from collection of IConfigurationSection

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

问题描述

我浏览了有关映射集合和嵌套映射以及嵌套集合的映射的文档,但是仍然无法解决我的情况。

我有以下json配置文件:

I've browsed documentation on mapping collections and nested mapping and mapping of nested collection, but still can't cope with my case.
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

非常感谢您的帮助!

===注意===

我仍​​然需要一个答案这个问题,但我在此处发布了具有更好解释的新问题

Would be very thankful for your help!
===Note===
I still need an answer for the question but I posted the new one with better explanation here.

推荐答案

此处实际上不需要Automepper。只需使用默认的.net核心绑定即可。

You don't actually need Automepper here. Just use default .net core binding.

重命名此名称

 public ICollection<CallConfiguration> CallsConfig { get; set; }

通话

然后使用类似的

 var config  = _configuration.GetSection("startupConfig:noSubscription").Get<FeedConfiguration>();

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

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