Reputation: 6796
I was facing an issue where I could use the .AfterMap() method or the .ForMember() method from AutoMapper. I was researching a little bit but did not find any information about it. What is the difference between them or when would you use one over the other?
AfterMap:
.AfterMap((src, dest) => dest.SomeDestinationProperty = src.SomeSourceProperty);
ForMember:
.ForMember(
dest => dest.SomeDestinationProperty,
opt => opt.MapFrom(src => src.SomeSourceProperty)
);
Upvotes: 2
Views: 3982
Reputation: 1943
AfterMap
is code that executes after AutoMaper has done its work. AutoMapper knows nothing about it (it is a black box) and cannot use any logic within it.
ForMember
specifies the mapping for a single member but the real magic happens when used with MapFrom
. In this combination AutoMapper knows exactly how to map one member to the other and it can automatically create a reverse mapping. It also allows you to use ProjectTo
in LINQ which will result in more optimal queries (especially if your DTO includes only few of the fields in your entity(s)).
Upvotes: 5