AutoMapper: what is the difference between ForMember and ForSourceMember?

I'm new to AutoMapper, so this is probably a beginner's question. I searched, but did not see this discussed. When creating a map, what is the difference between the ForMember and ForSourceMember methods:

Mapper.CreateMap<Role, RoleDto>() .ForMember(x => x.Users, opt => opt.Ignore()) .ForSourceMember(x => x.Users, opt => opt.Ignore()); 

I support code written by others. In some places I see ForMember, in other ForSourceMember and, as shown above, in one place.

What is the difference between the two?

Thanks in advance for any help.

+6
source share
1 answer

Look at the method signature. IN...

 Mapper.CreateMap<Role, RoleDto>() .ForMember(x => x.Users, opt => opt.Ignore()) .ForSourceMember(x => x.Users, opt => opt.Ignore()); 

... ForMember is a method that expects an Expression<Func<RoleDto>> parameter named destinationMember , while ForSourceMember expects an Expression<Func<Role>> parameter named sourceMember . So

  • ForMember configures the members of the target type.
  • ForSourceMember configures source type members.

In your case, both the source and target types have UserId members, so the calls look the same, but they are not. They should do the same, but the funny thing is that ForSourceMember doesn't seem to have any effect when ignoring members. Perhaps this is a mistake.

+7
source

All Articles