自动映射器:UseDestinationValue不适用于集合吗? [英] Automapper: UseDestinationValue does not work for collections?

查看:65
本文介绍了自动映射器:UseDestinationValue不适用于集合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设有一些课程:

public class Foo
{
    public List<Bar> Bars { get; set; }
}

public class Bar
{
    public string Name { get; set; }
    public string Description { get; set; }
}

public class FooDto
{
    public List<BarDto> Bars { get; set; }
}

public class BarDto
{
    public string Name { get; set; }
}

public class MapperProfile : Profile
{
    public MapperProfile()
    {
        CreateMap<FooDto, Foo>();
        CreateMap<BarDto, Bar>()
            .ForMember(dest => dest.Description, opt => opt.UseDestinationValue());
    }
}

如果我使用的是一对一映射,那么一切都会按预期进行:

If I'm using one-to-one mapping, everything is working as expected:

var mapper = new Mapper(new MapperConfiguration(e => e.AddProfile(typeof(MapperProfile))));

var bar = new Bar
{
    Name = "name",
    Description = "description"
};

var barDto = new BarDto
{
    Name = "updated name"
};

mapper.Map(barDto, bar);

Console.WriteLine(bar.Name);
Console.WriteLine(bar.Description);

// Output:
// updated name
// description

但这不适用于集合:

var mapper = new Mapper(new MapperConfiguration(e => e.AddProfile(typeof(MapperProfile))));

var foo = new Foo
{
    Bars = new List<Bar>
    {
        new Bar
        {
            Name = "name",
            Description = "description",
        }
    }
};

var fooDto = new FooDto
{
    Bars = new List<BarDto>
    {
        new BarDto
        {
            Name = "updated name"
        }
    }
};

mapper.Map(fooDto, foo);

foreach (var bar in foo.Bars)
{
    Console.WriteLine(bar.Name);
    Console.WriteLine(bar.Description);
}

// Output:
// updated name
//                

映射后,

Bar的 Description 为空(已在 9.0.0 10.0.0 版本上进行测试).

Bar's Description is null after mapping (have tested on 9.0.0 and 10.0.0 versions).

原因是什么,如何解决?

What is the reason, how to fix?

推荐答案

最后,我找到了解决方案.Automapper会在映射之前清除目标集合,这就是所有属性均设置为默认值的原因. AutoMapper.Collection 在这种情况下可以提供帮助.

Finally, I have found the solution. Automapper clears the destination collection before mapping, that is why all properties are set to default. AutoMapper.Collection can helps in this case.

public class Foo
{
    public List<Bar> Bars { get; set; }
}

public class Bar
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class FooDto
{
    public List<BarDto> Bars { get; set; }
}

public class BarDto
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class MapperProfile : Profile
{
    public MapperProfile()
    {
        CreateMap<FooDto, Foo>();
        CreateMap<BarDto, Bar>()
            .EqualityComparison((src, dest) => src.Id == dest.Id);
    }
}


并像这样使用:

var mapper = new Mapper(new MapperConfiguration(e =>
{
    e.AddCollectionMappers();
    e.AddProfile(typeof(MapperProfile));
}));

var foo = new Foo
{
    Bars = new List<Bar>
    {
        new Bar
        {
            Id = 1,
            Name = "name",
            Description = "description",
        }
    }
};

var fooDto = new FooDto
{
    Bars = new List<BarDto>
    {
        new BarDto
        {
            Id = 1,
            Name = "updated name"
        }
    }
};

mapper.Map(fooDto, foo);

foreach (var bar in foo.Bars)
{
    Console.WriteLine(bar.Name);
    Console.WriteLine(bar.Description);
}

// Output:
// updated name
// description

这篇关于自动映射器:UseDestinationValue不适用于集合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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