简体   繁体   中英

AutoMapper .AfterMap() vs .ForMember()

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)
);

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 mebmer 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 mappping. 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(ies)).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM