【问题标题】:AutoMapper map Dictionary with the help of injected service借助注入服务的 AutoMapper 地图字典
【发布时间】:2019-06-21 08:34:25
【问题描述】:

我需要这样的东西:

    public myMappingProfile(IInjectableService myInjectableService)
    {
        CreateMap<Source, Destination>()
            .ForMember(dest => dest.DestinationDictionary, opt => opt.MapFrom(src =>
            {
                var dict = new Dictionary<myEnum, string>();
                foreach (var item in src.SourceDictionary)
                {
                    dict.Add(item.Key, myInjectableService.BuildUrl(item.Value));
                }
                return dict;
            }));

服务的依赖注入工作正常。但 Visual Studio 显示以下错误消息:

带有语句体的 lambda 表达式不能转换为 表达树

然后我将目标类型从 Dictionary 更改为 List> 并尝试使用 AfterMap 方法:

           .ForMember(dest => dest.DestinationListOfKeyValuePair, opt => opt
           .MapFrom(src => src.SourceDictionary))
           .AfterMap((src, dest) => dest.DestinationListOfKeyValuePair
           .ForEach(ti => ti.Value = myInjectableService.BuildUrl(ti.Value)));

但 Visual Studio 抱怨:

无法将属性或索引器分配给 -- 它是只读的

下一个尝试是 CustomResolver:

.ForMember(dest => dest.TenantImages, opt => opt.MapFrom<CustomResolver>())

公共类 CustomResolver : IValueResolver>> { 私有只读 IInjectableService _myInjectableService;

    public CustomResolver(IInjectableService myInjectableService)
    {
        _myInjectableService = myInjectableService;
    }

    public List<KeyValuePair<MyEnum, string>> Resolve(
        Source source,
        Destination destination,
        List<KeyValuePair<MyEnum, string>> destMember,
        ResolutionContext context)
    {
        destMember = new List<KeyValuePair<MyEnum, string>>();
        foreach (var entry in source.SourceDictionary)
        {
            destMember.Add(new KeyValuePair<myEnum, string>(entry.Key, _myInjectableService.BuildUrl(entry.Value)));
        }
        return destMember;
    }
}

但是抛出了以下异常:

System.MissingMethodException:未定义无参数构造函数 对于这个对象。

我不知道如何将 IInjectableService 放入 CustomResolver。

任何想法如何解决这个问题? 谢谢。

【问题讨论】:

标签: c# dictionary dependency-injection automapper


【解决方案1】:

我认为你必须使用.ConvertUsing():

    // Add this mapping, if the name of the property in source and destination type differ.
    CreateMap<Source, Destination>()
        .ForMember(dest => dest.DestinationDictionary, opt => opt.MapFrom(src => src.SourceDictionary));

    // Add this mapping to create an instance of the dictionary, filled by the values from the source dictionary.
    CreateMap</*type of source dictionary*/, Dictionary<myEnum, string>>()
        .ConvertUsing(src =>
        {
            var dict = new Dictionary<myEnum, string>();
            foreach (var item in src)
            {
                dict.Add(item.Key, myInjectableService.BuildUrl(item.Value));
            }
            return dict;
        }));

此外,您可以将字典创建简化为 LINQ 单行:

src.ToDictionary(item => item.Key, item => myInjectableService.BuildUrl(item.Value));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2017-07-23
    • 2015-09-06
    • 2019-09-15
    • 1970-01-01
    相关资源
    最近更新 更多