Follow aggregate pattern when using automapper

When using automapper, we tried to create one mapper class per entity, and it quickly became out of control.

One of the reasons is that we thought Mapper.Reset() is good enough to do the pre-clean-up, but the CreateMap from another Mapper class can also re-define the mapping relationship. Single Mapper works OK in unit-test mode, but when merge together in application, too many mapper classes became too many interferences.

We decide to go with aggregate pattern to solve this mess, mapping should be defined in the “root” mapper class per aggregate. Simple, clean and easy to manage.

A simple rule will be one mapper class per repository class.

Another very useful tip for automapper I learned today is: instead of catching AutoMapperMappingException and look Context, it’s much easier just call this before do any actual map operation


It will give out more accurate error message in your mapping configuration, saved a lot of time. The message in context in exception is too general and useless.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s