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.
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.
Yes, or you can call CreateMap<ModelClass, ViewModelClass>(). ReverseMap() .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With