Cymen Vig

Software Craftsman

AutoMapper and Mapper.Reset()

Did you know that AutoMapper’s Mapper.CreateMap<T1, T2>() persists even if you use different options on the mapping? So if you have this in one place in your code:

And then later this:

That second usage won’t map BrainSize! This is quite confusing… The cache makes sense but it would be more intuitive if the cache was sensitive to .ForMember() usage and other options so that the exact same mapping is cached not any mapping.

The solution is to use this (potentially both before and after your use of AutoMapper):

I haven’t read the source yet but this is a very non-intuitive aspect of AutoMapper. Basically on the level of a deal breaker almost in my opinion.