AutoMapper How To Map Object A To Object B Differently Depending On Context

Posted by IanT8 on Stack Overflow See other posts from Stack Overflow or by IanT8
Published on 2010-04-29T00:49:04Z Indexed on 2010/04/29 0:57 UTC
Read the original article Hit count: 519

Filed under:

Calling all AutoMapper gurus!

I'd like to be able to map object A to object B differently depending on context at runtime. In particular, I'd like to ignore certain properties in one mapping case, and have all properties mapped in another case.

What I'm experiencing is that Mapper.CreateMap can be called successfully in the different mapping cases however, once CreateMap is called, the map for a particular pair of types is set and is not subsequently changed by succeeding CreateMap calls which might describe the mapping differently.

I found a blog post which advocates Mapper.Reset() to get round the problem, however, the static nature of the Mapper class means that it is only a matter of time before a collision and crash occur.

Is there a way to do this?

What I think I need is to call Mapper.CreateMap once per appdomain, and later, be able to call Mapper.Map with hints about which properties should be included / excluded.

Right now, I'm thinking about changing the source code by writing a non-static mapping class that holds the mapping config instance based. Poor performance, but thread safe.

What are my options. What can be done? Automapper seems so promising.

© Stack Overflow or respective owner

Related posts about automapper