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

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; }
}

I have two instances of Parent:
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" } }
};

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);

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".

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.

Any help would be much appreciated!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

It’s looking more and more likely that I will be unable to come up with mappings to handle this scenario; fair enough really as AutoMapper was never intended to be used to merge values in this way.

So I have resorted to handling the collection manually within a loop. For the purposes of this answer, I am assuming that Parent.A is uniquely identifying property:

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

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

Mapper.Map(parentOne, parentTwo);

foreach (var childTwo in parentTwo.Children)
{
    var childOne = parentOne.Children.Where(child => child.A == childTwo.A).Single();
    Mapper.Map(childOne, childTwo);
}

Method 3

I ran into this same problem when mapping from ViewModel (DTO) to EntitySet. Here’s the method that I wrote to solve the issue. It synchronizes a collection of ViewModels into a collection of Entities.

In your automapper mapping you will need to ignore the collection entirely.

public void SyncronizeEntitySet<TViewModel, TEntity>(IEnumerable<TViewModel> modelSet, ICollection<TEntity> entitySet,
        Func<TViewModel, int> sourceKey, Func<TEntity, int> destinationKey, Action<TEntity> setParentKey)
    where TViewModel : class, new()
    where TEntity : class, new()
{
    var toDelete = new List<TEntity>();
    foreach (var entityItem in entitySet)
    {
        var modelItem = modelSet.FirstOrDefault(i => sourceKey(i) == destinationKey(entityItem));
        if (modelItem == null)
        {
            toDelete.Add(entityItem);
        }
        else
        {
            Mapper.Map(modelItem, entityItem);
        }
    }

    toDelete.ForEach(i => Delete(i));

    foreach (var modelItem in modelSet)
    {
        if (sourceKey(modelItem) == 0)
        {
            var entityItem = Mapper.Map<TEntity>(modelItem);
            setParentKey(entityItem);
            Add(entityItem);
        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x