Don't Use AutoMapper in C#! Do THIS Instead!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
don't use automaper seriously just don't it's not only me who saying this even Jimmy pogard the automaper Creator says this here here and even here but why would you ask well there are a lot of reasons why we shouldn't use automaper but I am sure that you don't want to be that developer desperately looking for performance bottlenecks in an application just after the release right because that's exactly what could happen if you use automapper and guess which of these benchmarks runs with automaper I guess you already know that but wonder what is so fast in comparison well stay with me and I will show you [Applause] [Music] hey there and welcome to the code wrinkles channel for this brand new video before we get into the performance stuff on automaper and the problem was that it might cause and what better Alternatives we could have let's take a brief look into a very typical looking automaper configuration like this one now here we have a product and we have a product tto and we want to map from the product to the product dto and we specify here explicitly that for the destination name we want to map from the product name for the description we want to map from product description for the price well we want to map from the price but we also want to add a certain calculation now even though this code looks very very familiar and typical it is wrong in so many ways first off automamper as name implies is supposed to work automatically so it should be used when you can actually map automatically from one property to the other without specifying explicit four member calls and automaper indeed supports this because automapper should be possible or should be able to map the product name to the name the product description to the description without this four member calls so these four member calls are redundant in this case and then there is the next problem with this code which is this third four member call in which we will want to map from the price but we also specify a short calculation because in the product we have this price which is a price without a bet and we have of that percentage however in the product detail we have actually only the price so when we want to map from one to the other what we do is hey of course we can do this in the map from method so let's do the calculation here but does this smell like business logic because it is and it should be a total no-go and unfortunately this is something that we see very often when we look into automaper profiles that business logic kind of like is part or well is shown or is defined in the automaper profiles which shouldn't be there so this should be a total no-go cool now that we know okay we have some problems in the use or in the way that we use automaper usually and I will also want to talk about performance issues that we might have with auto mapper so we need to compare it to something so let's start also to look into some alternatives to automapper and by the way I will not take into considerations for this video other third part Library so I will just look into things that we can Implement just right of the Box just taking advantage oh well our programming skills and the features that we have in the c-sharp programming language so the first thing that we could do is kind of like we can specify our mappings directly in our classes so let's move over to the product class and see how we could Implement that the way that we can do this is just come to our product class and simply specify here a method that will return a product detail and let's call this two product detail and by the way this is an instance method so we would kind of like need the instance for that and we will return for from the instance from the product instance we will return a product detail and that could be something like that and as you can see here what we're doing here we say name is equal product name description for dto is product description and we do the calculation here which is not in automaper anymore it is right now part of our domain it's part of our model and this is okay and that's basically all we have just written five lines of code and we have configured basically our own mapping without the need for all the reflection stuff that we might use in Auto mapper however here you might say hey but I don't want to have these two product detail method here because this would imply that the product which is my domain model has knowledge about the dto which has a totally different concern and I would say okay no problem with that we can even go to the product detail and basically create the mapping there and the way this could work is let's go just here to the detail and let's move it here and let's add this method so public static in this case because from a static product detail from the class we want to create a new instance of the product detail by providing the product as an incoming parameter and what we do there is exactly the same we mapped the name from the product name the description from the product description and the price from the calculation that we do based on the price and the vat percentage that we have on the product so you are covered also in this case you have a mapping and right now basically you can go to the product detail and just reode to the product and remove this method and this means that the product will not have any knowledge about the detail any more and the dto itself since is probably part of the application layer or of an API layer or something like that it is okay that it has knowledge about the domain so that wouldn't be a problem cool now that we have set this up and have we have something to compare automapper with I would suggest us let's set up some benchmarks here and here's our Benchmark class let's go briefly through it so we have two preference field here first of all we have an array of product because in order to kind of like see the performance we want to do this operation on well a larger array of different products because this would simulate our application would behave if we get for instance thousands requests in one second or something like that so in this case we have this number of elements property and here we pass in some params for The Benchmark so first off we'll have 10 then we'll make the test on 100 and then we'll make the same tests or the same benchmarks on thousand and then we have this Global setup where we do initialize we do the mapper configuration it's exactly the same Emperor configuration that we have seen previously and then we create the mapper that we use for mapping and then of course we populate the product array with some products here and then what we do we have two benchmarks so first of all we have with automapper and then we have with direct assignment which is exactly what we had in this product like to product detail we have used right now for demo purposes the method to product detail from our product class but let's run this application let's make sure that we pass it into release because that's what benchmark.net needs and let's run the application and let's evaluate the results when they are ready these are the results and Benchmark results actually never lie so if we take a look with automapper we have these values and without Auto mapper or with direct assignment we have an outcome that is roughly twice as fast or twice faster than with auto mapper of course this is not a surprise because as we know automaper is using reflection under the hood and reflection is very expensive in terms of CPU usage and therefore our direct assignment is of course faster now if we take a look at these results where we have simulated like on on array with thousand products that's abruptly okay simulation or if you get thousand requests in your application what would happen and of course even in this case direct assignment of course is twice as fast so it's obvious that if you're using direct assignment and not using Auto mapper you are also gaining some performance and that might be important in application HotPads or in parts of the application that handle a lot of requests and need to do a lot of such mappings but what if I told you that there is even a faster way to achieve exactly the same thing well let's take a look into how we can accomplish this using one of the very nice and very well underrated I would say looking into different code bases c-sharp feature with which are the implicit and the explicit operators so let me get rid of this and let's start with the implicit operator now if we go to this idea of using an implicit operator we can simply go for instance um on our product and let's implement this implicit operator so we have this public static implicit operator and here we want to return a product detail and we want to have the product as a source and this is static because this is actually implemented statically and as we will see just in a second we can kind of like directly assign a product to a product detail and the conversion will be done implicitly by the compiler and it will return as this product detail and here we have the exact same logic like for the name of the product detail we map it from the product name the description we map it from the product description and well for the price of course we have our very small calculation here but in this case the calculation is in our model so this is good it is not in automaper anymore it is real logic that we have in our domain model so we can even unit test it very very easily and that's actually where the business logic is supposed to be when we go to our benchmarks and here we add a new Benchmark and we see say here is with implicit operator and this time what we do is we iterate of course through the array of products and for each product we say here simply we want a product detail so we need to be explicitly that we want this variable to be a product detail and then we just provide a product and here is where the implicit operator will kick in because it will look at the product it will check if it has its implicit operator that returns a product detail and yes it has one so it uses that operator to automatically transform basically our product to a product detail and of course this is a product detail now what we should do is of course we should run the benchmarks again and take a look at what actually is or if this makes any difference at all so let's run the application and we'll be right back when the benchmarks are finished these are once again The Benchmark results and if we take a look at it we can see that the width implicit operator is a little bit faster than the direct mapping and of course it's way faster than using it with auto mapper now if we look when we did this with a hundred products in the collection well it kind of like was still a little bit the same and when we have done with thousand products in the collection or in the array we see that once again uh with implicit operator it was a little bit faster but the difference is a little bit negligible I would say but still it is a little bit faster now you would argue once again that okay what I have done here is I have actually created here in this product let me go here to the product we have created this static implicit operator that returns a product detail so once again you might argue that well yes in this case the product does know about the product detail and this might not be okay and I totally agree with you so let's change that but before we we get further I just want to comment out this implicit operator and I want to go back to the product detail and no problem if we don't want to have this in the product let's add the logic in the product detail however since what we want to do the mapping we have an instance of the product we cannot use the implicit operator here directly on the product EQ so instead of doing this we just need to implement the explicit operator and once again the explicit operator would return this product detail and it would have the product as a source now the reason I had to comment out on the product that implicit operator is that when we want to use this operator since both operators the implicit on the product and the explicit on the prologic tool kind of like do the exact same mapping the compiler wouldn't know exactly what to use it would be an ambiguous reference so in that case we have commented out that one we have used here this one and it's exactly the same logic to implement here but right now the product doesn't even need to know anything about the the product details so let's go back to the Benchmark and let's maybe just also have here a little bit of refactoring this would be the explicit operator and then for each of our product what we want to do here we want to get a product detail but the way that we kind of like use those in Split explicit operators is by also providing for instance or by using a cast here the real cast to product detail and in this case we can just use here the VAR keyword and we should be good to go and if we run the benchmarks again right now it will do the mapping based on the explicit operator that we have provided in the product detail now the result will be very comparable to what we already had so that's why I won't run the benchmarks again but I just wanted to show you this kind of another possibility to also use the explicit operator not only the implicit operator and this gives you flexibility to kind of like choose exactly in the scenarios where you are you know what or which of the operators do you actually want to use cool now this being said actually uh the conclusion to this one is that okay maybe you should not just give up on using Auto mapper but what it is important on the other hand is to fully understand the extent to which Auto mapper can actually influence the performance of your application and also the different caveas that can occur for instance if you are using automaper as it is mostly used in a lot of code bases like we do a lot of custom mappings even for the properties that we don't need to do mappings because the auto mapper would kind of like do the mappings automatically as the name implies of course and then also the fact that in a lot of cases we might see scattered business logic in the profile configurations which is totally not okay from a lot of points of views and if you want to avoid that if you want to make sure that your application is more performant in that then just spend a little bit effort and Implement for instance a direct mapping or Implement in implicit or the explicit operators and you would be covered for most of the scenarios and don't give me the example that hey automapper is doing this faster because if you lose that time to actually write down all the configurations like the four member calls it's actually the same time that you would take to to write an implicit operator or an explicit operator or a direct assignment mapping so from from a Time consumption perspective using automapper the way that it is usually used in most of the code bases doesn't really give you anything and in terms of performance as you have seen actually without automapper we have way faster performance this being said if you enjoyed this video and you think that it might be useful for others don't forget to subscribe hit the thumbs up button on this video and also share it with your friends on your social network wherever you think that there might be anybody that would find this content useful share it and they would be grateful also if you have any questions don't be shy and hit the comment section of this video and leave me a comment I would be more than happy to get in touch with you and get a discussion going this being said once again thank you very much for watching and until the next time I wish you the very best
Info
Channel: Codewrinkles
Views: 47,712
Rating: undefined out of 5
Keywords: #programming, .net core, automapper, automapper asp.net core, automapper asp.net core 6, automapper asp.net core example, automapper asp.net core web api, automapper c#, automapper c# asp.net core, automapper dotnet, c#, c# tutorial, dtos, how to use automapper, how to use automapper in asp.net core, ipfs, map objects with automapper, mapping domain models with automapper, mapping dtos with automapper, programming, software architecture, software engineering
Id: -3BGBfvoUJM
Channel Id: undefined
Length: 16min 17sec (977 seconds)
Published: Wed Dec 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.