AutoMapper-为什么在DynamicMap上使用Map? [英] AutoMapper - why use Map over DynamicMap?

查看:182
本文介绍了AutoMapper-为什么在DynamicMap上使用Map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您要使用AutoMapper映射的对象不需要自定义映射,这样做是否有意义:

Assuming the objects you're mapping with AutoMapper require no custom mappings, is there ever a point in doing this:

Mapper.CreateMap<Src, Dest>(); 
// ....
Mapper.Map(SrcObject, DestObj);

如果不需要自定义映射,那么上述方法是否仅通过使用DynamicMap即可获得任何好处,而无需任何预先配置?

If no custom mappings are required, does the above approach gain you anything over just using DynamicMap without the need for any prior configuration?

Mapper.DynamicMap(SrcObject, DestObj);


当您映射匿名类型时,我确实理解DynamicMap是必需的 ,但是我问的是DyanmicMap是否曾经是不推荐使用的 不需要自定义映射的类型.


I do understand that DynamicMap is required when you're mapping anonymous types, but I'm asking about whether DyanmicMap is ever not preferred for static types that require no custom mappings.

推荐答案

自上次使用Automapper以来已有一段时间,但如果我没记错的话:

Been a while since I last used Automapper, but if I remember correctly:

要使用Map,您需要首先通过CreateMap显式指定那些Map.之后,您可以通过调用AssertConfigurationIsValid来验证您的配置.

In order to use Map, you need to specify those Maps explicitly first via CreateMap. Afterwards you can validate your Configuration by calling AssertConfigurationIsValid.

这是在启动应用程序后立即发生的,而不是在执行过程中遇到错误(建议您创建映射并在启动时进行验证).

This happens right after launching your application rather than encountering an error mid execution (given that you create the mappings and validate on startup, which is recommended).

还有一些类型与1:1不匹配,因此您需要指定确切的映射,不确定DynamicMap是否使用了您手动引入的Map,实际上我认为不正确./strike>
刚刚检查过,DynamicMap会回落到github上当前版本的现有Maps中.

Also some types do not match 1:1, so you would want to specify the exact mappings, not sure if DynamicMap makes use of the Maps you have introduced manually, actually I think it does not.
Just checked, DynamicMap falls back on existing Maps in the current version up on github.

这也是性能问题,因为DynamicMap使用反射的方式比Map方法使用更多的反射,因为您已经指定了映射配置,并且大多数不再需要动态声明.不过,目前还不确定新版本的Automapper是否正在执行这方面的缓存.

It's also a matter of performance since DynamicMap uses reflection more heavily than the Map method, since you have already specified the mapping configuration and most of it does not have to bee asserted on the fly anymore. Not sure if the new version of Automapper is performing caching in this regard by now though.

这篇关于AutoMapper-为什么在DynamicMap上使用Map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆