简体   繁体   English

AutoMapper:使用'忽略'在两个集合之间进行映射

[英]AutoMapper: Mapping between the two collections with 'Ignore'

I'm using AutoMapper to map between the two collections. 我正在使用AutoMapper在两个集合之间进行映射。 What I see is that the option Ignore is not working at this scenario as expected. 我看到的是Ignore在这种情况下没有按预期工作。 What i'm expecting can be see in the method AutoMapperIgnore_TwoObjectMappedWithIgnoreId_SameWithUnchangedIdAndNewPrice() . 我期待的可以在方法AutoMapperIgnore_TwoObjectMappedWithIgnoreId_SameWithUnchangedIdAndNewPrice()看到。 In other two test methods is the Id although ignored but every object in the collection is created once again with the consequences that original values are going to be lost. 在其他两个测试方法中,尽管忽略了Id但是集合中的每个对象都会再次创建,原因是原始值将丢失。 There is possibility to use UseDestinationValue , but I think this make only sense on the classes where collection are members of it. 有可能使用UseDestinationValue ,但我认为这只对集合所属的类有意义。 How can I use the option Ignore on the collections? 如何在集合中使用“ Ignore ”选项?

[TestClass]
public class AutoMapperTests
{
    private readonly IEnumerable<Dto> _testDtos;

    private readonly IEnumerable<Entity> _testEntities;

    public AutoMapperTests()
    {
        _testDtos = new List<Dto>
        {
            new Dto()
            {
                Id = 0,
                Fk_Id = 8,
                Price = 350000
            }
        };


        _testEntities = new List<Entity>
        {
            new Entity()
            {
                Id = 8,
                Price = 68000
            }
            ,
            new Entity()
            {
                Id = 6,
                Price = 350000
            }
        };
    }

    [TestInitialize]
    public void TestInitialize()
    {
        Mapper.Reset();
    }

    [TestMethod]
    public void AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntityIgnoreId_SameWithUnchangedIdAndNewPrice()
    {
        //Assign
        Mapper.CreateMap<Dto, Entity>()
            .ForMember(destination => destination.Id, opt => opt.Ignore());

        AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntity_SameWithUnchangedIdAndNewPrice(true);
    }

    [TestMethod]
    public void AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntityUseDestinationtValueForId_SameWithUnchangedIdAndNewPrice()
    {
        //Assign
        Mapper.CreateMap<Dto, Entity>()
            .ForMember(destination => destination.Id, opt => opt.UseDestinationValue());

        AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntity_SameWithUnchangedIdAndNewPrice(true);
    }


    private void AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntity_SameWithUnchangedIdAndNewPrice(bool isExceptedSame)
    {
        //Assign
        var exceptedPrice = _testDtos.First().Price;

        //Act
        IEnumerable<Entity> foundEntities = _testEntities.Join(_testDtos, e => e.Id, e => e.Fk_Id, (entity, dto) => entity).ToList();
        Entity entityBeforeMapping = foundEntities.First();

        Mapper.Map(_testDtos, foundEntities);

        Entity entityAfterMapping = foundEntities.First();


        //Assert
        if (isExceptedSame)
        {
            Assert.AreSame(entityBeforeMapping, entityAfterMapping);
        }
        Assert.AreEqual(entityBeforeMapping.Id, entityAfterMapping.Id);
        Assert.AreEqual(exceptedPrice, entityAfterMapping.Price);
    }

    [TestMethod]
    public void AutoMapperIgnore_TwoObjectMappedWithIgnoreId_SameWithUnchangedIdAndNewPrice()
    {
        //Assign
        Mapper.CreateMap<Dto, Entity>()
            .ForMember(destination => destination.Id, opt => opt.Ignore());

        var testDto = new Dto()
        {
            Id = 0,
            Fk_Id = 8,
            Price = 350000
        };

        var testEntity = new Entity()
        {
            Id = 8,
            Price = 68000
        };

        var exceptedPrice = testDto.Price;


        //Act
        Entity entityBeforeMapping = testEntity;
        Mapper.Map(testDto, testEntity);
        Entity entityAfterMapping = testEntity;


        //Assert
        Assert.AreSame(entityBeforeMapping, entityAfterMapping);
        Assert.AreEqual(entityBeforeMapping.Id, entityAfterMapping.Id);
        Assert.AreEqual(exceptedPrice, entityAfterMapping.Price);
    }

    internal class Dto
    {
        public int Id { get; set; }
        public int Fk_Id { get; set; }
        public Single? Price { get; set; }
    }

    internal class Entity
    {
        public int Id { get; set; }
        public Single? Price { get; set; }
    }
}

As @Stef mentioned you must map each entry in the collection individually, like this 正如@Stef所提到的,你必须单独映射集合中的每个条目,就像这样

_testEntities.Join(_testDtos, e => e.Id, e => e.Fk_Id, (entity, dto) => Mapper.Map(dto,entity));

Here the full functionally example: 这里有完整的功能示例:

[TestClass]
public class AutoMapperTests
{
    private readonly IEnumerable<Dto> _testDtos;

    private readonly IEnumerable<Entity> _testEntities;

    public AutoMapperTests()
    {
        _testDtos = new List<Dto>
        {
            new Dto()
            {
                Id = 0,
                Fk_Id = 8,
                Price = 350000
            }
        };


        _testEntities = new List<Entity>
        {
            new Entity()
            {
                Id = 8,
                Price = 68000
            }
            ,
            new Entity()
            {
                Id = 6,
                Price = 350000
            }
        };
    }

    [TestInitialize]
    public void TestInitialize()
    {
        Mapper.Reset();
    }

    [TestMethod]
    public void AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntityIgnoreId_SameWithUnchangedIdAndNewPrice()
    {
        //Assign
        Mapper.CreateMap<Dto, Entity>()
            .ForMember(destination => destination.Id, opt => opt.Ignore());

        AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntity_SameWithUnchangedIdAndNewPrice(true);
    }

    [TestMethod]
    public void AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntityUseDestinationtValueForId_SameWithUnchangedIdAndNewPrice()
    {
        //Assign
        Mapper.CreateMap<Dto, Entity>()
            .ForMember(destination => destination.Id, opt => opt.UseDestinationValue());


        AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntity_SameWithUnchangedIdAndNewPrice(true);
    }

    private void AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntity_SameWithUnchangedIdAndNewPrice(bool isExceptedSame)
    {
        //Assign
        var exceptedPrice = _testDtos.First().Price;

        //Act
        Entity entityBeforeMapping = _testEntities.First(testEntity => testEntity.Id == _testEntities.First().Id);
        IEnumerable<Entity> foundEntities = _testEntities.Join(_testDtos, e => e.Id, e => e.Fk_Id, (entity, dto) => Mapper.Map(dto,entity));
        Entity entityAfterMapping = foundEntities.First();


        //Assert
        if (isExceptedSame)
        {
            Assert.AreSame(entityBeforeMapping, entityAfterMapping);
        }
        Assert.AreEqual(entityBeforeMapping.Id, entityAfterMapping.Id);
        Assert.AreEqual(exceptedPrice, entityAfterMapping.Price);
    }

    [TestMethod]
    public void AutoMapperIgnore_TwoObjectMappedWithIgnoreId_SameWithUnchangedIdAndNewPrice()
    {
        //Assign
        Mapper.CreateMap<Dto, Entity>()
            .ForMember(destination => destination.Id, opt => opt.Ignore());

        var testDto = new Dto()
        {
            Id = 0,
            Fk_Id = 8,
            Price = 350000
        };

        var testEntity = new Entity()
        {
            Id = 8,
            Price = 68000
        };

        var exceptedPrice = testDto.Price;


        //Act
        Entity entityBeforeMapping = testEntity;
        Mapper.Map(testDto, testEntity);
        Entity entityAfterMapping = testEntity;


        //Assert
        Assert.AreSame(entityBeforeMapping, entityAfterMapping);
        Assert.AreEqual(entityBeforeMapping.Id, entityAfterMapping.Id);
        Assert.AreEqual(exceptedPrice, entityAfterMapping.Price);
    }

    internal class Dto
    {
        public int Id { get; set; }
        public int Fk_Id { get; set; }
        public Single? Price { get; set; }
    }

    internal class Entity
    {
        public int Id { get; set; }
        public Single? Price { get; set; }
    }
}

I did some tests and confirmed your issue. 我做了一些测试并确认了你的问题。

When a IEnumerable list is mapped, AutoMapper creates a new IEnumerable list which only includes the properties you define to map, so only the 'Price'. 映射IEnumerable列表时,AutoMapper会创建一个新的IEnumerable列表,该列表仅包含您定义要映射的属性,因此只有“Price”。

In order to map a Dto to an Entity, you need to define a property which makes them unique, like a PrimaryKey. 为了将Dto映射到实体,您需要定义一个使它们唯一的属性,如PrimaryKey。

class Dto
{
    public long PrimaryKey { get; set; }
    public int Id { get; set; }
    public int Fk_Id { get; set; }
    public Single? Price { get; set; }
}

class Entity
{
    public long PrimaryKey { get; set; }
    public int Id { get; set; }

    public Single? Price { get; set; }
}

Mapping a List to a List can be done like this: 将列表映射到列表可以这样做:

// Update entities in original list
foreach (var d in _testDtos)
{
    foreach (var e in _testEntities)
    {
        if (e.PrimaryKey == d.PrimaryKey)
        {
            Mapper.Map(d, e);
        }
    }
}

Or more linq-friendly: 或更多linq友好:

// Create a new list
var foundEntities = _testEntities.Join(_testDtos, e => e.PrimaryKey, d => d.PrimaryKey, (entity, dto) => Mapper.Map<Entity>(dto)).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM