AutoMapper: Merging values of items in a collection from one instance to another

后端 未结 3 1355
一生所求
一生所求 2020-12-19 16:55

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:



        
3条回答
  •  一个人的身影
    2020-12-19 17:18

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

    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.

提交回复
热议问题