Merging Collections produces empty

23 views
Skip to first unread message

Mark Davis

unread,
Nov 18, 2016, 7:57:02 PM11/18/16
to AutoMapper-users
Not sure what I'm doing wrong here.  I'm attempting to use a custom IValueResolver to merge a source member collection with an already existing destination member collection rather than simply replace the destination's collection but the resulting collection has no items in it.  I had something similar working in older versions of AutoMapper that sent/returned a ResolutionResult to/from the Resolve method.  I would set resolutionResult.ShouldIgnore = true since the destination collection was already all fixed up and this worked well.  In 5.x, I'm now simply returning the original destination member but it's not working.

The end goal is similar to that of http://internetexception.com/post/2013/05/25/Mapping-collection-of-entities-in-EF-with-AutoMapper.aspx but that solution, like my previous one, is based on the older IValueResolver interface.


using AutoMapper;
using System.Collections.Generic;
namespace AutoMapperTests
{
   
public class AutoMapperTester
   
{
       
public class Source
       
{
           
public ICollection<string> Collection { get; set; }
       
}
       
public class Destination
       
{
           
public ICollection<string> Collection { get; set; }
       
}
       
public class MergeCollectionResolver : IValueResolver<Source, Destination, ICollection<string>>
       
{
           
public ICollection<string> Resolve(Source source, Destination destination, ICollection<string> destMember, ResolutionContext context)
           
{
               
foreach (string sourceItem in source.Collection)
               
{
                    destMember
.Add(sourceItem);
               
}
               
return destMember;
           
}
       
}
       
public void Test()
       
{
           
Mapper.Initialize(cfg =>
           
{
                cfg
.CreateMap<Source, Destination>()
                   
.ForMember(s => s.Collection, opt => opt.ResolveUsing(new MergeCollectionResolver()));
           
});
           
var source = new Source()
           
{
               
Collection = new List<string>() { "a", "b", "c" }
           
};
           
var destination = new Destination()
           
{
               
Collection = new List<string>() { "d", "e", "f" }
           
};
           
Mapper.Map<Source, Destination>(source, destination);
           
// After mapping the destination collection has 0 items.
           
// It should have 6.
           
System.Diagnostics.Debug.Assert(destination.Collection.Count == 6);
       
}
   
}
}




Jimmy Bogard

unread,
Nov 18, 2016, 7:58:49 PM11/18/16
to automapp...@googlegroups.com
GitHub issue?

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Davis

unread,
Nov 18, 2016, 8:22:41 PM11/18/16
to AutoMapper-users
Not sure what you mean.  I've tested this with both the NuGet package and the GitHub source code and found the same results.  I'm not able to trace into Resolve's caller in VS as it's a dynamically generated method.  That would of course show me where the destination member is being set but c'est la vie.  Am I correct in assuming that the caller simply sets the destination member to the value returned by of Resolve?  I'm actually not even changing said value.  I'm just adding items to it. 

Mark Davis

unread,
Nov 18, 2016, 8:29:17 PM11/18/16
to AutoMapper-users
I'll post to GitHub as well if you think it's a bug.  I just thought maybe I was doing something wrong.

Jimmy Bogard

unread,
Nov 18, 2016, 9:38:06 PM11/18/16
to automapp...@googlegroups.com
Ah no i meant can you open an issue, sorry :)

On Fri, Nov 18, 2016 at 7:29 PM Mark Davis <nyd...@gmail.com> wrote:
I'll post to GitHub as well if you think it's a bug.  I just thought maybe I was doing something wrong.

--

Mark Davis

unread,
Nov 19, 2016, 5:13:37 PM11/19/16
to AutoMapper-users
Actually I figured that out right after I posted.  I've submitted the issue on GitHub.

And BTW, thanks for creating and supporting such a useful library.  It's been an immense time saver.
Reply all
Reply to author
Forward
0 new messages