The 3 Biggest Mistakes of Object Mapping in .NET

preview_player
Показать описание


Hello, everybody, I'm Nick, and in this video, I will show you what I consider to be the top 3 mistakes that people do when they are using object mapping. This isn't limited to using libraries like Automapper but it's also applicable to manual mapping too.

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

I find it perfectly acceptable to implement mapping in the ctor, PROVIDED that the reverse direction is not required or is defined in a method underneath. It would not be worthwhile to overhaul the entire architecture for this.
My approach would be the same: Simply add a 'MapToEntity' method below the constructor.

The main reason for this is: If the DTO needs adjustment - which indeed occurs more frequently than with the DB model - you inevitably have to check the mapping as well. These two components are closely tied and mutually dependent, and in my opinion, they belong together.

Of course, it's accurate to state that the DTO doesn't need to know anything about the mapping, but there is also (almost) nothing to know, particularly if we adhere to your first rule and avoid business logic in the mapping. The developers working on it can save time searching for the mapping and are less likely to forget it because they can adjust the mapping directly in the DTO. Therefore, it either prevents errors or at least saves time.

I also appreciate the philosophy behind Mapster. Although I haven't personally worked with it, if it maps one-to-one and flags mapping issues at compile-time, I don't see a reason why it shouldn't be used.

Palladin
Автор

you always release videos about topics right after i start using things 😂

myuuiii
Автор

This is all a bit above me at the moment, but I do love how animated you are over it!! As my skills increase I dare say I'll come back to this video.

AthelstanEngland
Автор

I think the extension method conversion approach is better and I prefer it. But I don't see anything necessarily wrong with your DTO knowing the about the Domain object.
However, I think automatically Mapping DTOs to Domain objects is a bad practice in general because there might be business constraints and decisions to be made so that the domain objects are not in the wrong state. The forward mapping is fine but the backward mapping should be deliberate and intentional like in a business logic method or subfunction with proper validation and constraints

odytrice
Автор

Completely agree on all the points. I have seen all 3 mistakes, mostly 1 and 3rd on few projects. I just can stand that mappers can have dependencies. The good point in favor of manual mapping is that it's much harder to pass dependencies in the static method rather in the mapping profile

antonmartyniuk
Автор

Do you have a video about how to do mapping the right way?

ocnah
Автор

Thank you so much for sharing Nick!
This is exactly what I searched for. Extension methods is the way to go.

torrvic
Автор

I completely agree with all of these rules. Mentioned mistakes follow from the Single Responsibility principle violation. It is a great example for such a violation in places where you do not expect them to be at all. Awesome video as always! Thank you for your content!

yuGesreveR
Автор

I really don't see a problem with the third method. In my mind, the purpose of the DTO is to transfer data and get it to where it's going, which is in its final form, and so having mappers in the DTO actually makes sense. Besides, this way you can see in one place the DTO definition, what it is supposed to be mapped to, and how it is mapped.

DaStopher
Автор

Thanks for the hints. I personally sometimes mock my mapper and do not use the profile directly. It feels always wrong, and now I know it was wrong

sacharkin
Автор

Manual written static mappic classes in conjunction with readonly DTOs are easy and fast to implement, everything is explicit, they are easy to understand and easy to maintan. A few years ago I worked in a project, where we used AutoMapper and it was much more complicated than manual mapping, so I see no ebenfit in using AutoMapper or similar libraries

nertsch
Автор

now I feel proud of myself because I came up with the extension method idea by myself one day. But thanks I've seen other mappers you mentioned in some of the project and never realized whats wrong with them.

thescarletcars
Автор

I’ve tended to use the extension method based approach you show towards the end. It is debug-able, straightforward and does not require knowledge of a 3rd party library.
Solid advice about unit tests and avoiding mock of auto mapper.

SudhanshuMishraTheOne
Автор

Really cool topic, had several issues in my mind that Nick mentioned, now I've changed my outlook. Thank you!

mykolakriukov
Автор

I sometimes used the constructor on DTO to construct it out of the domain object. In general it works fine ... for sure there is no reverse direction but that is normally not required. There are many cases where you need to map domain object to a DTO to provide some sort of a 'view' into the domain. However reverse is usually needed in some sort of an action (Create/Update methods which receive Create/Update DTOs) and there I would usually manually create/update the domain object.

Автор

I'd worked in a project with a lot of business logic in AutoMapper, it was a nightmare to mantain

VitorSouza-cpxr
Автор

Its a common mistake among developers to use and create multiple tools to make it faster to code. The problem is that the priorities are now different, exliciitness and readability are the top priorities leasing to less errors and easier maintenance

dmitrykim
Автор

About the first mistake with business logic. Doest that mean that the password should be left empty in the mapper and set in the service?

rosenpetrov
Автор

BTW SonarCube has a concern about the last approach with extension. It says that you should move that extension should be moved as a method to the result class (if both are located in the same project).

MoskowGoAwayFromUa
Автор

You have uncovered a couple of issues with one of my projects!
We use automapper but occasionally map objects “manually”.
Plus I believe there are a couple of places where we have some inserted business logic.
Thanks for this!

brian