Automapper ResolveUsing or MapFrom

Angad picture Angad · Sep 2, 2014 · Viewed 29.4k times · Source

I have a mapping definition defined as

Mapper.CreateMap<Calculator, CalculatorViewModel>()
  .ForMember(dest => dest.TypeIndicator, src => src.ResolveUsing(new TypeIndicatorResolver()));

Should I be using ResolveUsing or MapFrom(src => SomePrivateMethod()) ?

What is the difference between ResolveUsing and MapFrom when it comes to complex mapping.

The Resolver or Private method will go to the database and get a value.

Answer

Jimmy Bogard picture Jimmy Bogard · Sep 3, 2014

MapFrom uses Expressions, while ResolveUsing uses a Func. MapFrom only allows redirection of properties:

ForMember(d => d.Foo, opt => opt.MapFrom(src => src.Bar.Baz.Foo))

ResolveUsing can be anything

ForMember(d => d.Foo, opt => opt.ResolveUsing(src => HitDatabaseWithStuff(src));

I'd use a Resolver class when the resolution logic needs to be shared amongst more than one member, or if I want to have the resolver instantiated by a service locator. Otherwise, a private method is fine.