DEV Community

Discussion on: Learn how YOU can convert your Objects between layers with Automapper, C#, .NET Core and VS Code

Collapse
 
costinmanda profile image
Costin Manda

I had to work on a project that used AutoMapper and I hated it. Yes, it has a lot of features and as you move from similar models and entities to more complex scenarios it has support for most of them, but debugging becomes a nightmare. Here are the issues I have with AutoMapper:

  • it uses reflection, so you can't see which properties are being used and which are not
  • since it maps properties with the same names, it promotes isomorphism between models and entities, which is an antipattern
  • it's hard to determine the source of bugs and then to subsequently debug problems in the AutoMapper configuration
  • it hinders testability

My solution: use dependency injection and inject IMapper when you need mapping. Implement those mappers however you see fit (using, if you must, AutoMapper).

I changed all automapping with this kind of solution in my project and never looked back. It allowed for async mapping, injection of services when I needed extra information (like turning some id into a value or getting details from additional source) and everything was clearly defined in terms of intent and implementation as well as unit testable at every level.

Collapse
 
softchris profile image
Chris Noring

hi Costin. I appreciate you writing this down. I've never faced that much of a problem as you describe above. I think most libraries have a sweet spot where they shine vs don't shine