AutoMapper:集合中从一个实例合并项的值,以另一 [英] AutoMapper: Merging values of items in a collection from one instance to another

查看:1575
本文介绍了AutoMapper:集合中从一个实例合并项的值,以另一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何使用AutoMapper合并两个复杂的对象实例。父对象有一个属性是子对象的集合:

I am trying to figure out how to merge two complex object instances using AutoMapper. The parent object has a property which is a collection of child objects:

public class Parent
{
    public List<Child> Children { get; set; }
}

public class Child
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
}

的两个实例:

var parentOne = new Parent()
{
    Children = new List<Child>() { new Child() { A = "A", B = "B", C = "C" } }
};

var parentTwo = new Parent()
{
    Children = new List<Child>() { new Child() { C = "Updated value" } }
};

我希望能够从 parentOne 值合并 parentTwo ,不会覆盖<$值>ç$ C> C 在 parentTwo

I would like to be able to merge values from parentOne to parentTwo, without overwriting the value of C in parentTwo. The maps I have created are as follows:

Mapper.CreateMap<Parent, Parent>()
    .ForMember(parent => parent.Children, opt => opt.UseDestinationValue());

Mapper.CreateMap<Child, Child>()
    .ForMember(child => child.C, opt => opt.Ignore());

Mapper.Map(parentOne, parentTwo);

据我了解,AutoMapper将除非你使用 UseDestinationValue()选项来创建复杂属性的新实例。但是,在执行上面的code后, parentTwo.C 等于C而不是更新值

As I understand it, AutoMapper will create new instances of complex properties unless you use the UseDestinationValue() option. However, after executing the code above, parentTwo.C equals "C" instead of "Updated value".

在我看来像它的保持名单,LT的实例;儿童&GT; ,但它正在创建儿童的新实例的名单。不幸的是,我挣扎拿出一张地图,将让每个实例儿童以及

It looks to me like it's keeping the instance of List<Child>, but it is creating new instances of Child within the List. Unfortunately, I'm struggling to come up with a map that will keep each instance of Child as well.

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

推荐答案

据我所知,AutoMapper不支持这一点,我相信这是因为支撑这种情况的复杂性。

As far as I know, AutoMapper doesn't support this and I believe it's because of the complexities of supporting such a scenario.

该UseDestinationValue确实只对集合实例工作,你猜测 - 而不是收集要素。在你的情况下,只有一个在每个列表项和(我认为)你想同步更新子列表对象(即第一个元素更新第一要素,第二个更新第二,等...)。但是,如果一个列表为3和其他列表中有5?什么是UseDestinationValue的意思是,如果没有目的地的价值?你如何选择哪desintation对象中的两个子列表中映射?在数据库方案(这听起来好像是这个问题的基础)将有唯一的ID或某种外键这将让你孩子匹配对象知道映射哪一个。在这种情况下,它太硬(IMO)的写一个通用的映射,将支持UseDestinationValue对集合的单个元素。

The UseDestinationValue does indeed only work on the collection instance as you surmised -- not the collection elements. In your scenario, there is only one item in each list and (I assume) you want child list objects updated "in sync" (i.e. first element updates first element, second updates second, etc...). But what if one list as 3 and the other list has 5? What does "UseDestinationValue" mean if there is no destination value? And how do you pick which desintation object to map among the two child lists? In a database scenario (which it sounds like was the basis for the question) there would be unique ids or some kind of foreign key which would allow you to match up the child objects to know which one to map to. In this case, it's too hard (IMO) to write a generic mapping that would support UseDestinationValue on individual elements of a collection.

这篇关于AutoMapper:集合中从一个实例合并项的值,以另一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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