Reading about ReversMap() , I can’t understand the difference between ForMember and ForPath . The functionality that ForPath performs on this page I usually do with ForMember .

Here is an example where I configure two-way mapping and everything works correctly:

 public class Customer { public string Surname { get; set; } public string Name { get; set; } public int Age { get; set; } } public class CustomerDto { public string CustomerName { get; set; } public int Age { get; set; } } static void Main(string[] args) { Mapper.Initialize(cfg => { cfg.CreateMap<Customer, CustomerDto>() .ForMember(dist => dist.CustomerName, opt => opt.MapFrom(src => $"{src.Surname} {src.Name}")) .ReverseMap() .ForMember(dist => dist.Surname, opt => opt.MapFrom(src => src.CustomerName.Split(' ')[0])) .ForMember(dist => dist.Name, opt => opt.MapFrom(src => src.CustomerName.Split(' ')[1])); }); // код с маппингом Customer -> CustomerDto //... // // код с маппингом CustomerDto -> Customer var customerDto = new CustomerDto { CustomerName = "Shakhabov Adam", Age = 31 }; var newCustomer = Mapper.Map<CustomerDto, Customer>(customerDto); } 

Question

Methods ForMember and ForPath in something duplicate functionality? Or what can not be done with ForMember , but with ForPath ?

    1 answer 1

    It is easy to find out by looking at the source .

    ForMember does not perform any checks and processes any expression with IMemberConfigurationExpression .

      public IMappingExpression<TSource, TDestination> ForMember<TMember>(Expression<Func<TDestination, TMember>> destinationMember, Action<IMemberConfigurationExpression<TSource, TDestination, TMember>> memberOptions) { var memberInfo = ReflectionHelper.FindProperty(destinationMember); return ForDestinationMember(memberInfo, memberOptions); } 

    ForPath works only with IPathConfigurationExpression and throws an exception for other members.

     public IMappingExpression<TSource, TDestination> ForPath<TMember>(Expression<Func<TDestination, TMember>> destinationMember, Action<IPathConfigurationExpression<TSource, TDestination>> memberOptions) { if(!destinationMember.IsMemberPath()) { throw new ArgumentOutOfRangeException(nameof(destinationMember), "Only member accesses are allowed."); } ... 

    If you need the functionality described by the IPathConfigurationExpression interface, use ForPath , otherwise ForMember

    An example of using the Ignore() method on the same page on your link:

     cfg.CreateMap<Order, OrderDto>() .ForMember(d => d.CustomerName, opt => opt.MapFrom(src => src.Customer.Name)) .ReverseMap() .ForPath(s => s.Customer.Name, opt => opt.Ignore());