Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper ResolveUsing or MapFrom

Tags:

automapper

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.

like image 700
Angad Avatar asked Sep 02 '14 17:09

Angad


People also ask

When should I use AutoMapper?

AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.

Does AutoMapper map both ways?

Yes, or you can call CreateMap<ModelClass, ViewModelClass>(). ReverseMap() .

What is AutoMapper good for?

AutoMapper is a simple library that helps us to transform one object type into another. It is a convention-based object-to-object mapper that requires very little configuration. The object-to-object mapping works by transforming an input object of one type into an output object of a different type.


1 Answers

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.

like image 100
Jimmy Bogard Avatar answered Oct 11 '22 05:10

Jimmy Bogard