DEV Community

Cover image for Learn how YOU can convert your Objects between layers with Automapper, C#, .NET Core and VS Code

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

Chris Noring on January 22, 2020

When we read/write data we need to transport our information from one layer to the next. Our medium of transportation is usually by storing our da...
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 .NET

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

Collapse
 
lftrejo profile image
lftrejo

What’s the advantage of using this instead of implicit operator or explicit operator?

Collapse
 
victorioberra profile image
Victorio Berra • Edited

Automapper has more quality of life features like built in mapping validation and helpful errors and it protects you against forgetting to map properties. Also it makes testing your app much easier since it's got the built in DI stuff so you could technically inject a mock mapper.

It's whole purpose is to make your life easier when mapping objects. You should use whatever works best for you.

Collapse
 
thfsilvab profile image
Thadeu

Thank you for making this question, I am really curious about it too.

Collapse
 
binarypatrick profile image
BinaryPatrick • Edited

I recently found automapper and am really sold on the ease of use and power it offers. It's a really great product.

I am a bit confused about the DI piece though. I don't get why you need to specify the startup class or why it works in that way? I've never seen DI like that. I've kinda gone my own way and created a Singleton I inject on my own instead of using their functionality.

My question is, what is the DI doing with startup class? I can't see a benefit as I still have to define my maps.