4

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.

CitizenBane
  • 855
  • 7
  • 17
  • 1
    Not an answer to your question, but a comment: You can also unit test runtime. if you fear to accidentially forget such a conversion and deliver an application that is slow in one point, you can add a Timeout-Parameter that fails the test if it runs more that X seconds. – Michael Stum Sep 19 '09 at 16:02

2 Answers2

1

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.

Jimmy Bogard
  • 26,045
  • 5
  • 74
  • 69
  • Thanks, I wasn't sure if I had to convert things back like I did above. The conversions do "Just work", it's just that If I don't initially create a map, it's much much slower. – CitizenBane Sep 19 '09 at 14:18
  • Ok, so i DO have to create the vice-versa maps - damn, I need more coffee. – CitizenBane Sep 19 '09 at 14:30
0

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.

CitizenBane
  • 855
  • 7
  • 17