14

I am using Automapper to transfer data from objectA to objectB

classe ObjectA
{
   string Title;
   string Summary;
}

classe ObjectB
{
   string Title;
   string Summary;
   IAddress Address;
}

I created this kind of mapping between the two objects

AutoMapper.Mapper.CreateMap<IObectA, IObjectB>()
      .ForMember(dest => dest.Title,           src => src.MapFrom(s => s.Title))
      .ForMember(dest => dest.Summary,         src => src.MapFrom(s => s.Summary))
      .ForMember(dest => dest.Address,         src => src.Ignore())

I create my ObjectB and fill all its properties including Address

When I call the mapper, I was expecting it to override Title and Summary and ignore Address

ObjectB = Mapper.Map<IObjectA, IObjectB>(objectA); 

Actually, it is throwing and exception for Address.

What am I doing wrong?

[UPDATE] To express it differently, I have my objectB and I want to update part of it with data coming from ObjectA. When I say Ignore, I mean leave the data the way they already are

user385411
  • 1,445
  • 1
  • 18
  • 35
  • it might be useful to know what exception you received... – JoDG Mar 21 '11 at 08:00
  • It seems you are doing everything wrong. Your code example shows the class definition for ObjectA twice. Later you say you fill ObjectB with all its properties, but in the last code snippet it shows you are trying to map objectA to objectB, not the other way around. – JoDG Mar 21 '11 at 08:17
  • @JoDG, My Mistake. I updated my initial post. threw an exception of type 'System.Collections.Generic.KeyNotFoundException' – user385411 Mar 21 '11 at 08:39
  • I have no idea where the exception comes from. Your actual question seems to be identical to this one however: [http://stackoverflow.com/questions/3672447/how-do-you-map-a-dto-to-an-existing-object-instance-with-nested-objects-using-aut](http://stackoverflow.com/questions/3672447/how-do-you-map-a-dto-to-an-existing-object-instance-with-nested-objects-using-aut) – JoDG Mar 21 '11 at 09:12
  • Yes, but it is very less complicated case How to update an existing object instance using AutoMapper? – user385411 Mar 21 '11 at 09:33
  • 1
    additionally to automapper you could use valueinjecter, with it this operation would be just ObjectB.InjectFrom(ObjectA) without any configuration at all – Omu Mar 22 '11 at 20:30

1 Answers1

36

I found the solution.

I just discovered that Map method has an overloaded version that excepts a pre-instantiated destination object. Thanks to this article

Mapper.Map<IObjectA, IObjectB>(objectA, ObjectB ); 

@JoDG, Thank you for your help

Alex
  • 14,104
  • 11
  • 54
  • 77
user385411
  • 1,445
  • 1
  • 18
  • 35
  • 3
    The article linked to does not exist anymore... :( – noocyte Mar 08 '13 at 08:09
  • 2
    @noocyte : Archive.org has your back! http://web.archive.org/web/20130124102758/http://www.dominicpettifer.co.uk/Blog/45/automapper---a-custom-type-converter-that-exposes-a-destination-value – James P. Wright Jul 02 '13 at 18:33
  • 3
    just as a note, you shouldn't need to specify the types in <> Mapper.Map(objectA, ObjectB); Should be enough – DomenicDatti Oct 08 '13 at 17:41