The Best .NET Mapper to Use in 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and in this video we're going to take a look at which object mapper in.net is the fastest most memory efficient and easiest to use and also discuss some of the common mistakes people make when they use a mapper and whether you need one in the first place and spoiler alert you probably don't if you lack of content and you want to see more make sure you subscribe during the sub notification Bell and for more training check out nickjobsers.com alright so let me show you what I have here I have a dotnet 8 application over here and I'm going to show you the map as we're going to be benchmarking in this video so we have Auto mapper of course everyone knows it then mobster which I've made a video in the past and it's already in version 7 so great progress then we have maperly and we have tiny mapper now some of you might be watching this and saying hey Nick you missed agile mapper and express mapper and you would be right if the authors of those packages bother building them for release mode since both of them on nuget Under full releases are debug mode builds I'm not going to include them now Brillo spoiler even if they were release modes they wouldn't be equally as fast as what we're gonna see in this video however that's why I excluded them you have to at least put the effort in to build it in release mode to get that optimized code out now let me show you what I have in the project what we're going to be mapping from and to is this dto so we have this Spotify album API response object over here with a bunch of things like nesting the Rays nested objects strings integers long objects and we're going to map that to this model over here now why are we mapping well we're mapping because you might have a dto sort of for your database or a dto for your API contract and then you might have a domain object or an application object or a model that should be detached from those two objects because those things are versioned and you want to be able to make any changes you want in your domain so you just have this mapping layer converting from one object to the other and then you can do all the changes you want in that middle layer without affecting any of the sides which are contracts at least that is the most common scenario I've seen now half people misused mappers to do other things sure when we're gonna talk about that later in the video but the main idea is you have model layers you have model B they look similar or almost identical you can map from one to the other without having to write manual code and say album type this equals album type that in this new object and then I've generated this test data object with a bunch of stuff in here just to make it more realistic and see some of the most complicated and heavy scenarios that need mapping so we have everything here now let's see what the process of mapping looks like for each of the mappers so first we have of course automaper now automaper used to have the static approach of mapping from one object to another but now everyone is using this imapper interface that is injected into your classes needing mapping and then you can Define your configurations you can say that this object or this Source object can be mapped to this destination object and vice versa so we have two-way mapping here from one to the other and from the other to one and then you just say create mapper and you have this Auto mapper object you can use to do dot map and map from one to the other then tiny mapper goes with a more static approach so this is how you would Define the exact same thing then mobster doesn't need any configuration you can just figure it out on its own which is fantastic and then mopper lip all you have to do is actually say new maperley mapper and that is it now the interesting thing about most mappers is that they're based on reflection or Expressions so they're required to do some heavy work during runtime most of the time to make the processing happen and make the mapping happen so you don't have to write your code mappingly has a different approach it actually Source generates mappers based on the models you want to map so for example all I have to Define for marketing is to say that this is a mapperly class and then slap a mapper attribute on it and as long as I have a partial class and a partial method it will go ahead and generate the implementation for me so it will go behind the scenes and press F12 to go definition it's going to make all that for me without me having to write any code which is an excellent approach and it's why we have search generators to make things like this happen also if there's a compiling time issue with this approach you're gonna get it because the source generator will break your build you wouldn't know about things like the mappers here unless your application runs or you have tests to validate that and have the tests fail before you even deploy so it is an excellent approach in my opinion and map really has actually become my favorite mapper topping mobster which also has a code generation version but as we're gonna see maperley has a trick up its sleeve now two other things I want to mention is the manual mapping so you have two forms of it first we can have a generated mapper and in this case we still used a library it's more of a visual studio plugin actually we used mapping generator to generate this so in Visual Studio you can actually install a plugin which can see an object and generate all this code using Roselyn in your ID as if it was a normal refactoring so you can just say Okay generate a mapper for this to this and it's going to write the code for you in your IDE so that's what that plugin will spit out and then at the bottom I wrote my own mapper and I tried to also make it as efficient as I could so we have the same logic as before but we're making the most out of the arrays we have in here and we have four loops and everything just to make sure it's as fast and as memory efficient as it can be and that is it obviously this is the code you'd have to write before so you can see that going from all this to something like this is pretty beneficial for the quality of your code base if you want to go back and make a small change it can be very very tricky and in terms of the mapping itself Auto mopper looks like this a tiny mopper like this then mobster like this and remember it doesn't need any configuration so that's all you really need from Lobster which is great in my opinion same from rapidly all you have to have in place is this class and that's that and then we have the generated map using mapping generator and my manual mapping so what I'm going to do is first just run the Benchmark and we have a memory diagnosure to see the memory impact of that execution so I'm going to say Benchmark Runner over here and run the benchmarks release mode and let's see what we get back alright so results are back and let's see what we have here so let's go from the slowest and work our way up so first we have tiny mapper with 1.5 microseconds and 2.1 kilobytes of memory it's bad especially considering that automatic has the same amount of required configuration be a bit more more efficient but twice as fast so super happy with this now everything above this is very interesting because mapping generator which is that plugin that generated that compile time code is very memory efficient 1.1 kilobytes compared to almost double that and it's of course very very fast 203 nanoseconds but it is not as fast as mobster which is not compile time generated I have to say this is actually a very impressive of mapster but mapperly just wins when it comes to mappers it is just so incredibly easy to create the mapper if you want to remember what Maple looks like it's just this you just have the password class partial method map from what to what then the mapper attribute and that is it and you can have as many methods as you want in here in this monthly mapper so for me that is my new favorite mapper for what it's doing and then you have the manual mapper which I wrote being the fastest bike just a little bit and I think what really makes it the fastest in this case is the lack of methods so the fact that it's just a sequential method and doesn't have to jump into any other methods to to make that frosting happen and even though potentially some of them will be in line from what I understand are by the jet it is not as fast but it's equally as memory efficient so extremely impressive I think the biggest point you need to keep here is the difference between mobility and mapping generator because mapping generator has a very interesting value proposition it's very memory efficient and it's also very fast but because it is using things like do array let me just go over here and show you it has things like this and select statements it is going to be less memory efficient and slower even with all the performance improvements that link has so if you're making something like mapping generator then at least make it efficient and don't use Link yes using link will make it easier for the developer to write the thing if you want to be as fast as the fastest thing you can manually write since it's a machine writing the code you should just make it write fast code now that's all fine but the real question is do you even need a mapper in the first place and as with everything in programming it really depends what I need to clarify is what you should not be doing using a mapper you should not be mocking the mapper in your unit test that's a very big one if you inject an eye mapper in your unit test and you're mocking that mapper you're doing something wrong you should not do that just implement the mapper as it is in your obligation and if you say that oh I can't do that because then in my mapper I'm actually injecting an interface that causes like that's the problem you're putting logic in your mapper that shouldn't be in your mapper if you are having business logic complicated logic if you're injecting things into your mapper you're 99 of the time doing something wrong that shouldn't be in your mapper and it should be somewhere else the mapper has a single job to do take object a and use all that information to map it into object B if you have to make a database call or call a service or do something else to make that other object happen then that should not be in your mapper don't do that that's why they have a bad rep now if your mapper is sort of view model to model mapping which is what automapper was originally created to do then absolutely use the mapper you can see how much code you can save and not only that with Source generators you can have something that is so low overhead or actually no overhead for in that case in my opinion sure use it but the moment things start getting complicated and you have the feeling of saying I should inject something here or use a service or use something then at that point is where you have to take a step back and say okay I'm doing something wrong here that does not belong in my mapper in my cases I'm always between either writing my own mapper or using something like mapperly a source generated version you can actually see and it has the guarantees that something like this would have on compile time which is very important to me now I should point out for those wondering that map really does support configuration so if you want to have aftermath before map factories sort of customized mapping you can do that with this version as well but keep in mind that the further you deviate from the core of the functionality which is the mapping the more likely you are to make a mistake so what's the conclusion what do I think about all this well if you are to use the mapper you use rapidly in my opinion it is the best and it doesn't really get better than that however make sure that you don't put any logic in the mapper that doesn't belong in the map will be very very careful but now I want to know from you are you actively using a mapper and what do you think about it have you migrated from the mapper to another one leave a comment down below and let me know well that's all I had for you for this video thank you very much for watching special thanks to my patreons making it as possible if you want to support me as well you can find the link description down below leave a like if you like this video subscribe like this in the Bell as well and I'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 51,231
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, dotnet, .net, .net 7, The best .NET Mapper to use in 2023, automapper, mapster, .net mapper, .net object mapper, mapper, object mapper, mapperly, agilemapper, tinymapper, expressmapper, automapper vs mapster, automapper vs manual mapping
Id: U8gSdQN2jWI
Channel Id: undefined
Length: 11min 56sec (716 seconds)
Published: Mon Mar 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.