-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Description
While I was trying to find a solution to issue #3129, I came across two related problems.
I spent a couple hours looking at the AutoMapper code and found no easy way to fix.
I guess it needs a little more work and insight and I could use some help.
Basically there are 3 things, that need a fix:
-
ReverseMap()
works in combination withMapFrom(...expression...)
, but doesn't with
MapFrom(...sourceMemberName...)
.So this works as expected:
cfg.CreateMap<Client, ClientDto>() .ForMember(d => d.Id, opt => opt.MapFrom(s => s.LocalId)) .ReverseMap();
But this does not work:
cfg.CreateMap<Client, ClientDto>() .ForMember(d => d.Id, opt => opt.MapFrom("LocalId")) .ReverseMap();
I spotted the problem, basically the implementation(s) of IPropertyMapConfiguration.Reverse() do
not support the case without a source expression. -
The issue Attribute-based Reverse Mapping with SourceMember does not work #3129 addresses that the reverse mapping does not work properly when using the AutoMapAttribute together with the SourceMemberAttribute. There are two reasons. One reason is the problem 1, which I just stated and the next one is, that AutoMapper handles the SourceMemberAttribute's after the reverse mapping has been created.
If I switch the order, all unit tests run through, so I guess it comes down to fixing problem 1. -
When creating a
ReverseMap()
withMapFrom(...expression...)
on a destination member and if there is a source property with the same name as the destination property, it will get reverse mapped aswell. So a ClientDto with Id = 10 will reverse map to a Client with Id = 10 and LocalId = 10. I do not expect that, because it should be a 1 to 1 mapping, right?
To fix that we would need some Ignore() for the reverse mapping, when using MapFrom.
I don't have a solution, yet.
So here are the classes:
public class Client
{
public int LocalId { get; set; }
public int Id { get; set; }
}
[AutoMap(typeof(Client), ReverseMap = true)]
public class ClientDto
{
[SourceMember("LocalId")]
public int Id { get; set; }
}