views:

686

answers:

2

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.

+1  A: 

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
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
Ok, so i DO have to create the vice-versa maps - damn, I need more coffee.
CitizenBane
So, can this "just works" conversion be turned off, or not?
CitizenBane
A: 

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