如何关闭 AutoMapper 在 List和 List之间的自动列表转换和EntitySet

发布于 2024-08-05 22:10:02 字数 659 浏览 2 评论 0原文

我使用 AutoMapper 将 Linq-To-SQL 生成的实体映射到 DTO。

当我最初创建单元测试时,我设置了特定的映射(通过静态配置类)将一种类型的 EntitySet 转换为通用列表(反之亦然)。

Mapper.CreateMap<EntitySet<Member>, List<MemberDTO>>(); 
Mapper.CreateMap<List<MemberDTO>, EntitySet<Member>>(); 

删除列表转换后(在确定 AutoMapper 将转换这些是自动的),我的单元测试仍然有效,但速度慢得像爬行一样。它的速度非常慢,执行每个测试实际上需要一分钟。

重新添加列表映射后,单元测试恢复了正常的性能速度。

有没有办法关闭列表的自动转换,以便我必须映射我的列表转换?如果我未能包含地图,我希望它抛出 AutoMapperException。我想避免这些性能问题。

如果情况变得更糟,我可能最终会编写一个快速代码生成模板来根据 DTO 自动创建我的映射配置类。这样我就不会错过任何事情。

谢谢。

I'm mapping my Linq-To-SQL generated entities to DTOs using AutoMapper.

When I initially created the unit tests, I had specific maps (through a static configuration class) set up to convert one type of EntitySet to a generic List (and vice-versa)

Mapper.CreateMap<EntitySet<Member>, List<MemberDTO>>(); 
Mapper.CreateMap<List<MemberDTO>, EntitySet<Member>>(); 

Upon removing the list conversions (upon figuring out that AutoMapper will convert these automatically), my unit tests still worked, but they slowed to a crawl. It was so noticeably slow, it took literally a minute to perform each test.

After re-adding the list mappings, the Unit Tests resumed their normal performance speeds.

Is there a way to turn this auto conversion for lists off, so that I HAVE to map my list conversions? I'd like it to throw an AutoMapperException if I fail to include a Map. I'd like to avoid these performance issues.

If worse comes to worse, I might just end up writing a quick code generation template to automagically create my mapping configuration class based off of the DTOs. That way, I won't miss anything.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞慾 2024-08-12 22:10:02

每个 AppDomain 中的任何 CreateMap 调用都不需要发生多次,包括测试和生产代码。我们有一个静态方法,双重检查锁定以确保配置只发生一次。配置是静态缓存的,因此无需多次执行。

然而,假设您有会员 -> ,这些转换应该“正常工作”。 MemberDTO 和反之映射设置。我将为 List<> 运行几个冒烟测试。 ->实体集<>以确保其正常工作。

Any CreateMap call does not need to happen more than once per AppDomain, that includes both tests and production code. We have ours in a static method, double-check locked to ensure configuration only happens once. Configuration is cached statically, so there's just no need to do it more than once.

However, these conversions should "just work", assuming you have the Member -> MemberDTO and vice versa maps set up. I'll run a couple of smoke tests for List<> -> EntitySet<> to make sure it works OK.

┼── 2024-08-12 22:10:02

事实证明你不能只是关闭自动转换。我几乎到处都看过,但似乎没有真正的方法可以做到这一点。

与此同时,我正在手写所有的映射。

It turns out you can't just shut off the auto-conversion. I've looked just about everywhere, and there seems to be no real way to do it.

In the meantime, I'm writing all the mappings out by hand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文