忽略采用IValueResolver的ResolveUsing
重载,只查看以下两种方法:
void ResolveUsing(Func<TSource, object> resolver);
void MapFrom<TMember>(Expression<Func<TSource, TMember>> sourceMember);
这两个之间的主要区别似乎是ResolveUsing
采用Func<TSource, object>
,而MapFrom采用Expression<Func<TSource, TMember>>
。
然而,在实际使用这些方法之一和lambda表达式的客户端代码中,它们似乎是可互换的:
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));
那么,以上两种选择的最终区别是什么呢?一个比另一个快吗?一个比另一个更好吗?如果是,什么时候/为什么?
过去,我在邮件列表上与Automapper的作者进行了长时间的电子邮件交流。MapFrom将通过表达式进行所有空检查
所以你可以做
opt => opt.MapFrom(src => src.SomeProp.Way.Down.Here.Somewhere)
,每个级别都会被检查对于null(就像它已经对扁平化所做的那样)。
我刚刚使用新的C#空条件运算符?.
进行了一些基准测试
考虑以下场景:类A
有一个子类B
,它有一个子C
,我们希望将其Name
属性展平为DTO。我测试了两种变体:
// using mapfrom
CreateMap<MapFromA, MapFromADto>()
.ForMember(dto => dto.Name, o => o.MapFrom(a => a.B.C.Name));
// using resolveusing with elvis
CreateMap<ResolveUsingX, ResolveUsingXDto>()
.ForMember(dto => dto.Name, o => o.ResolveUsing(x => x.Y?.Z?.Name));
我为1000个不同的ResolveUsingX x
和MapFromA a
调用了_mapper.Map<ResolveUsingXDto>(x);
或_mapper.Map<MapFromADto>(a);
,并使用System.Diagnostics.StopWatch
花了一些时间。以下是我的结果:
Distinct elements per batch: 1000; # batches for average: 25
A->B->C.Name, C is never null.
MapForm - average time taken for 1000x: 5527,84 ticks = 1,44 ms.
ResolveUsing - average time taken for 1000x: 5479,76 ticks = 1,4 ms.
A->B->C.Name, C is null 1/3 of the time.
MapForm - average time taken for 1000x: 72924,4 ticks = 27,44 ms.
ResolveUsing - average time taken for 1000x: 5351,2 ticks = 1,48 ms.
A->B->C.Name, C is null 1/2 of the time.
MapForm - average time taken for 1000x: 107016,92 ticks = 40,52 ms.
ResolveUsing - average time taken for 1000x: 5835,32 ticks = 1,56 ms.
A->B->C.Name, C is null 2/3 of the time.
MapForm - average time taken for 1000x: 141437,96 ticks = 53,64 ms.
ResolveUsing - average time taken for 1000x: 5789,72 ticks = 1,56 ms.
MapFrom
必须捕获NullReferenceException,这比使用elvis运算符?.
的ResolveUsing
慢
MapFrom
有一些额外的智能。例如(来自邮件列表):
在MapFrom中,我尽量聪明地挖掘子属性(很像普通的平坦化)。MapFrom是一种模仿扁平化的尝试,增加了一点允许重定向的功能。ResolveUsing没有此行为。
我不确定这是否在任何地方都有完整的文档(除了源代码中)。
尽管在许多情况下都可以使用这两种方法,但根据官方文档,在LINQ投影方面存在差异。详细的解释可以在这里找到。
长话短说:尽可能使用MapFrom。
根据源代码,ResolveUsing
更为复杂。源值可以是任何对象;因此,您可以使用任何想要填充目标成员的值,例如通过"解析"给定对象获得的int或bool。但是,MapFrom
仅使用成员从进行映射。
/// <summary>
/// Resolve destination member using a custom value resolver callback. Used instead of MapFrom when not simply redirecting a source member
/// This method cannot be used in conjunction with LINQ query projection
/// </summary>
/// <param name="resolver">Callback function to resolve against source type</param>
void ResolveUsing(Func<TSource, object> resolver);
/// <summary>
/// Specify the source member to map from. Can only reference a member on the <typeparamref name="TSource"/> type
/// This method can be used in mapping to LINQ query projections, while ResolveUsing cannot.
/// Any null reference exceptions in this expression will be ignored (similar to flattening behavior)
/// </summary>
/// <typeparam name="TMember">Member type of the source member to use</typeparam>
/// <param name="sourceMember">Expression referencing the source member to map against</param>
void MapFrom<TMember>(Expression<Func<TSource, TMember>> sourceMember);