使用AutoMapper合并对象 [英] Using AutoMapper to merge objects

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

问题描述

我正在尝试使用AutoMapper合并来自多个对象的数据,并且遇到了一些似乎无法解决的问题.

I'm attempting to use AutoMapper to merge data from multiple objects, and I'm running into a couple issues that I can't seem to solve.

我有一个像这样的对象:

I have an object like:

public class Parent
{
    public string Id { get; set; }
    public List<Child> Children { get; set; }
}
public class Child
{
    public string Key { get; set; }
    public int? Value1 { get; set; }
    public int? Value2 { get; set; }
    public int? Value3 { get; set; }
    public int? Value4 { get; set; }
    public int? Value5 { get; set; }
}

很明显,子属性并非全部都是int,但大多数都是可空的.

Obviously the child properties are not all ints, but most of them are nullable.

我在应用程序的两个不同层中都有这些对象,因此我使用AutoMapper在它们之间进行转换:

I have these objects in two different tiers of my application, so I'm using AutoMapper to convert between them:

{
    Mapper.CreateMap<Parent, ParentDTO>();
    Mapper.CreateMap<ParentDTO, Parent>();

    Mapper.CreateMap<Child, ChildDTO>();
    Mapper.CreateMap<ChildDTO, Child>();
}

转换效果很好,我对转换的效果感到满意.但是,现在我需要合并多个相同类型的对象.我将拥有一个对象的一个​​副本,该对象具有大多数或全部的属性,而另一个副本只有几个,其余均为null.我希望将第二个(部分)对象的任何非null属性映射到第一个(已填写)对象中的相应字段.正如对此答案的公认答案所言,我也应该能够使用AutoMapper来执行此操作,但是它不会给出任何明确的例子.

The conversion works well, and I'm happy with what it does. However, now I have a need to merge multiple objects of the same type. I will have one copy of an object which has most or all of the properties populated, and another copy which only has a few, with the rest being null. I want any non-null properties of the second (partial) object to be mapped to the respective field in the first (already filled out) object. As the accepted answer to this answer states, I should be able to use AutoMapper to do this as well, but it doesn't give any clear examples.

但是,当我执行该操作时,得到的对象与两个对象中的任何一个都相同,没有按照我的意愿进行组合.

However, when I go to perform the operation, I get an object that is identical to either of the objects, not combined like I want.

{
    var bigParent = new Parent
    {
        Id = "14",
        Children = new List<Child>
        {
            new Child
            {
                Key = "A",
                Value1 = 10,
                Value2 = 20,
                Value3 = 30,
                Value4 = 40,
                Value5 = 50
            }                   
        }
    };

    var merge = new Parent
    {
        Id = "14",
        Children = new List<Child>
        {
            new Child
            {
                Key = "A",
                Value1 = null,
                Value2 = null,
                Value3 = 100,
                Value4 = null,
                Value5 = null
            }
        }
    };

    var res = Mapper.Map(merge, bigParent);
}

我期望Child的值分别为10、20、100、40和50.但是,取决于我是否将合并作为源或目标放置在Mapper.Map中,我会得到null,null,100,null,null或10,20,30,40,50.

I am expecting Child to have values of 10, 20, 100, 40 and 50. However, depending on if I put merge as source or destination in Mapper.Map I get either null, null, 100, null, null or 10, 20, 30, 40, 50.

有没有一种方法可以获取我的期望值?我在想拥有列表是导致问题的原因,因为它不知道如何排列实体(确定它们是否相同).如果is回答了任何问题,则可以通过查看一个或多个属性(在此示例中为Key)是否相同来确定子记录是否相同.

Is there a way I can get my expected values? I'm thinking having the List is what is causing the issues, since it won't know how to line up the entities (to determine if they are the same or not). If is answers any questions, I would be able to identify if child records are the same by seeing if one or more properties are the same (in this example, Key).

推荐答案

如果要忽略在AutoMapper配置文件中定义的映射中的空值,请使用:

If you want to ignore null values in mappings defined in AutoMapper Profile, use:

public class MappingProfile : AutoMapper.Profile
{
  public MappingProfile()
  {
     this.CreateMap<Parent, Parent>()
         .ForAllMembers(o => o.Condition((source, destination, member) => member != null));
  }
}

这篇关于使用AutoMapper合并对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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