Ignoring the ResolveUsing
overloads that take an IValueResolver, and looking only at these 2 methods:
void ResolveUsing(Func<TSource, object> resolver); void MapFrom<TMember>(Expression<Func<TSource, TMember>> sourceMember);
The main difference between these 2 seems to be that ResolveUsing
takes a Func<TSource, object>
, whereas MapFrom takes an Expression<Func<TSource, TMember>>
.
However in client code that actually uses one of these methods with a lambda expression, they seem to be interchangeable:
Mapper.CreateMap<SourceType, DestType>() // uses ResolveUsing .ForMember(d => d.DestPropX, o => o.ResolveUsing(s => s.SourcePropY)); Mapper.CreateMap<SourceType, DestType>() // uses MapFrom .ForMember(d => d.DestPropX, o => o.MapFrom(s => s.SourcePropY));
So what ultimately is the difference between the above 2 choices? Is one faster than the other? Is one a better choice than the other and if so, when / why?
CreateMap<EFAddress, Address>() . ForMember(dest => dest. Code, opt => opt. MapFrom(src => src.Name));
When you call CreateMap, AutoMapper uses optimizers to build the code for getting/setting values on source/destination types. Currently, it uses a combination of Reflection.
AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.
In the past I had a long email exchange on the mailing list with the author of Automapper. MapFrom will do null checks all the way trough the expression:
So you can do
opt => opt.MapFrom(src => src.SomeProp.Way.Down.Here.Somewhere)
and each level will get checked for nulls (as it already does for flattening).
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