Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper : partial mapping

Tags:

automapper

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

like image 887
user385411 Avatar asked Mar 21 '11 07:03

user385411


1 Answers

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

like image 169
user385411 Avatar answered Sep 18 '22 17:09

user385411