AutoMapper:从多个源属性映射到集合

时间:2016-10-27 09:36:09

标签: c# automapper

更新2018年4月13日:Automapper 6.1.0通过引入ReverseMap支持取消发布。请参阅发行说明here

我正在尝试使用AutoMapper来取消对象。

我有一个来源如下

public class Source
{
    public string Name {get;set;}
    public string Child1Property1 {get;set;}
    public string Child1Property2 {get;set;}
    public string Child2Property1 {get;set;}
    public string Child2Property2 {get;set;}
}

我想将此映射到目的地

public class Destination
{
    public string Name {get;set;}
    public List<Child> Children {get;set;}
}

public class Child
{
    public string Property1 {get;set;}
    public string Property2 {get;set;}
}

我的映射配置

public static class AutoMapperConfiguration
{
    public static MapperConfiguration Configure()
    {
        var config = new MapperConfiguration(
            cfg =>
            {
                cfg.CreateMap<Source, Destination>()
                    .ForMember(dest => dest.Children, /* What do I put here?*/))
                // I don't think this is correct
                cfg.CreateMap<Source, Child>()
                    .ForMember(dest => dest.Property1, opt => opt.MapFrom(src => src.Child1Property1))
                    .ForMember(dest => dest.Property2, opt => opt.MapFrom(src => src.Child1Property2))
                    .ForMember(dest => dest.Property1, opt => opt.MapFrom(src => src.Child2Property1))
                    .ForMember(dest => dest.Property2, opt => opt.MapFrom(src => src.Child2Property2));

            });
        return config;
    }
}

现在当我测试我的代码时,我使用mapper.Map<List<Child>>(source)我得到了一个AutoMapperMappingException: Missing type map configuration or unsupported mapping.这是有意义的,因为没有配置到List<Child>的映射。如果我mapper.Map<Child>(source),我会获得一个Child个实例,其中包含属性的所有null值。

我很遗憾无法修改Source类。

AutoMapper可以实现这一点吗?如果是这样的话?

3 个答案:

答案 0 :(得分:2)

至少有两个选项。您可以使用简单的扩展方法来简化映射,也可以创建自定义类型转换器。

df1 = df[df.Timing == 17].reindex_axis(columns, axis=1)

答案 1 :(得分:0)

您可以在Source类上添加一个方法来获取子列表。 然后它很容易映射。

答案 2 :(得分:0)

与其使用自定义类型转换器,不如使用custom value resolver并将其余映射留给AutoMapper可能更好。在这种情况下,将source.Name映射到destination.Name并不困难,但想象一下您还有AutoMapper可以处理的其他10个属性,或者可以使用默认的opt.MapFrom

示例自定义值解析器:

public class SourceToDestinationChildResolver : IValueResolver<Source, Destination, List<Child>>
{
    public List<Child> Resolve(Source source, Destination destination, List<Child> member, ResolutionContext context)
    {
        destination = destination ?? new Destination();
        destination.Children = destination.Children ?? new List<Child>();
        destination.Children.Add(new Child() { Property1 = source.Child1Property1, Property2 = source.Child1Property2 });
        destination.Children.Add(new Child() { Property1 = source.Child2Property1, Property2 = source.Child2Property2 });
        // This is not needed then
        // destination.Name = source.Name;
        return destination.Children;
    }
}

使用解析器的配置:

public static class AutoMapperConfiguration
{
    public static MapperConfiguration Configure()
    {
        var config = new MapperConfiguration(
            cfg =>
            {
                cfg.CreateMap<Source, Destination>()
                   .ForMember(dest => dest.Children, opt => opt.MapFrom<SourceToDestinationChildResolver>())
            });
        return config;
    }
}

可以List<Child> member的正确使用方式来帮助我自己阐明解决方案。在文档中对我来说不清楚,所以请有人发表评论:)

相关问题