0

I have a list on the target object that I want to reconcile against the source.

The list is already created, I just want AutoMapper to add/update/delete items from this list, at no time should the list be set to a new object reference. How can I do this?

The reason for this is that the target object is an ORM generated code, and for whatever reason it has no setter.

The target looks like this:

 public class Target
 {
      //This is the target member.
      public List EntityList
      {
           get{
                return _entityList;
           }
      }

      //Except this is a much more complicated, track-able list
      private List _entityList= new List(); 
 }
Alwyn
  • 8,079
  • 12
  • 59
  • 107

1 Answers1

0

From my experience, AutoMapper isn't really designed with mapping onto already populated objects in mind. But one way to do what you're asking is to configure AutoMapper to ignore the EntityList property for mapping, then use AfterMap to call some custom reconciliation function you write to align the two lists:

Mapper.CreateMap<Source, Target>()
              .ForMember(dest => dest.EntityList, opt => opt.Ignore())
              .AfterMap((src, dest) => Reconcile(src.EntityList, dest.EntityList));
plmw
  • 321
  • 1
  • 8