Mapster, the best .NET mapper that you are (probably) not using

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

Hello everybody I'm Nick and in this video I wanna talk about a package called Mapster. It's a mapping library that it is packing some really amazing features that I wanted to share with you. No only that but it also performs increadibly well, outperforming all other mappers.

Timestamps

Intro and personal take - 0:00
Benchmarks of existing mappers - 1:57
Simple mapping with Mapster - 6:04
Code generation with Mapster - 13:50

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

Social Media:

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

Which mapper are you using? Do you think there is another mapper with some amazing features that I can try? Leave a comment and let me know
EDIT: Mapster also supports code generation without the use of Attributes. I highly recommend checking their great documentation on the matter. If you are interested I can make a deep dive video showcasing it.

nickchapsas
Автор

Before 24:01 I still wanted to use Automapper and after watching the benchmark result I would say I changed my mind. Really interesting comparison. Thanks Nick!

peymannaji
Автор

I used mapping for a while and stopped. Two deal breakers - 1. Lose the ability to find all usages/references to a property/field using the IDE. 2. Can no longer rename/refactor fields safely without relying on everyone writing comprehensive tests. While tests are great, I never want to have to rely on them for correctness more than necessary (type system is better). They are a safety net, but fallible nonetheless.

stochastic
Автор

This is veeery interesting indeed! The code generation is sweet!
One thing I would be afraid of though, would be that if I add anything to my domain object it will automatically be propagated into the DTO too, which is not something I might want, especially if the domain object is also autogenerated.
Scenario: EF Core Database first - we scaffold the context and table model classes with a tool, and generate the .cs files.
Then mapper would then just create 1:1 DTOs
Usually in situations like this we want to have DTOs defined and map them from domain object, which I see mapster still does better than Automapper.

Need to dive deeper into this one :)

DemoBytom
Автор

22:25 If you want even more performance, you should use an array, because you know the size beforehand. Also 'foreach' can actually be faster than 'for' if the iterator is a ref struct, because of array boundary checks. Better than ref struct iterator you must go unsafe, but i think array iterator is ref struct and implemented in unsafe code so it is as fast as it gets. (I may be wrong about array iterator implementation tho)

IceQub
Автор

Wow, this looks fantastic!
Won’t be long before we will have static codegen for a lot of things that required reflection, like DI.

nitpick: the code at the end could pass a capacity to the new list, thus not having to double its internal array whenever it was exceeded.
This is a very low hanging fruit with sometimes big impact w/o readability downsides.
The other thing was doing an index lookup more than once instead of using a local var.
But I got the point: It will probably do it it that way automatically...

Robert-G
Автор

Mapster without config seems to be the best option, very fast and easy to use.
To improve the speed on the manual mapping you should set capacity of the new options list.
The advanced mapper that requires attributes on the database object seems very cumbersome.

casperhansen
Автор

In your optimisation you could have declared the capacity of the new list since you already knew the source list size, which would have saved the resizing of the internal size of the list.

possessedllama
Автор

That code generation feature of mapster is amazing

kblyr
Автор

Thanks for this video, I'm using Automapper since 2013 and all other mapper's implementation (including manual) felt like trying to run in mud. I think I'm going to refactor an old project, after I benchmark some expensive transformation I made in the past.

DiomedesDominguez
Автор

What a good video, very well explained and with the benchmark and justifications, thank you very much Nick, I love the content of your channel

claudiopedalino
Автор

Definatly will try in a new home project. Looks great. Great video and explanation. Never come across the benchmarking tool before. Will also play around with that.

JohnMitchell
Автор

Very interesting video, thank you Nick.

I feel that the point of DTO and translated objects are to have 100% control of how they look, since they might be part of external APIs.

So adding a new property to an internal model object should not just appear on the DTOs. How would you best control that while still using Mapster? (Bit lazy now, I could go to the docs of course, but I suspect more in this audience wonders)

dbug
Автор

In your manual mapping code you should initialize List of options with the known collection size to avoid extra memory allocation or copying.

ZlobnyiSerg
Автор

Many thanks, for the explanation how mapster working. Thes result of benchmarks is excellent.

AlexeyLopatin-mr
Автор

Thanks man, your content is always great.

soltanalkhatib
Автор

For any newbies in here, I'd suggest skipping the mapping libraries entirely. They aren't necessary and they hide important code. Instead, write the mapping code manually where it's needed. If you start to duplicate mapping code, pull it into a separate shared class. It's that simple. You don't need a library :)

smithcolin
Автор

Hi, whats the point of having DTOs to be generated ? They will have always the same shape as the source object. If I just want to send the serialized DTO over the wire, I don't see any benefit of the generated code besides the little bit better performance.

biglufik
Автор

Why use same dto models as source though?
Wouldn’t you want to run code gen once and then edit it as you want?
If i understand correctly, every build would rewrite Models folder files with a newly generated code.

KoScosss
Автор

This is interesting .. Thank you so much Nick!

markyanthonylaredo