.NET 6 πŸš€ AutoMapper & Data Transfer Objects (DTOs)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this little web development tutorial I'm Patrick God and I am sweating today because it is freaking hot in here 30 degrees Celsius 86 Fahrenheit yeah look this up so uh yeah let's make this quick it's about Auto mapper and data transfer objects and that's pretty much it it's a little introduction into these things and I think this is pretty useful stuff so if you learned something and like this tutorial I would really appreciate it if you hit the like button and maybe even subscribe to my channel it does make a difference thank you so much and also please check out my newsletter because in there you get all these tutorials here on my YouTube channel earlier in your inbox and also access to upcoming online courses and discounts and all that stuff so if you want to get this then maybe you want to subscribe to my newsletter but apart from that just continue watching and enjoy this little introduction to automapa with.net6 alright so I created this little web AP Pi Project here with.net6 and visual studio 2022 and you can see already I did not change a lot I just added a superhero class here and also a superhero controller really nothing fancy in what we want to do here is we see this entity with an ID the name first name and so on and also a controller with some mock data already where we get these Heroes here now we can already start this again it's pretty simple stuff let me show you here Swagger UI we've got our call we can try this out hit execute and we get the complete data now the thing is that maybe we don't want to show the users some parts of this data here of this entity like the date added for instance date modified maybe even the ID not necessary so let's say we only want to display the name the first name and the last name and the place and this is where the dto comes in the data transfer object meaning we create another class so let's go back to visual studio and then we just call this superhero dto for instance so right click the project add a new class here and again we call the super hero dto for instance and typically you would use this for communicating with the client with the front end right so you've got a superhero dto here in this example and since this should be a really simple example we just copy these properties here paste them and that's it already but let's say the application gets a bit more complex maybe then you may find some classes called superhero response or superhero results or also superhero request for instance when you want to create a new superhero with specific data let's say we've got a lot more data like some I don't know some statistics maybe some strength data whatever it is and in this case the request would be then used to create a new superhero and the response or the result detail then for the result and we will do this here as well with again a really short and hopefully simple example but we will use the superhero dto here for both the response and also the request but let's start with the response right so we've got our superhero dto here now the thing is to get this data here in this call what we would have to do is we would we would have to go through the complete list here and map the properties one by one right so in essence let's say we would have um a superhero dto or something like that which is a new superhero dto right and then we would then say I know we would we would run through the year we would iterate through the um through the list here but let's say we've got uh a superhero here and then we would have to say all right so the ID or no we would just want to of course tell the send the name to the client it's already pretty late guys and it's it's really freaking hot in here it is uh well let's tell this so it's 30 degrees Celsius here in my little studio and what's that in Fahrenheit let me let me have a quick look Celsius too Fahrenheit [Music] 86 yeah maybe almost 90 Fahrenheit well not really 89 maybe so that would be 32 Celsius so it's really really hot and it's late so yeah I'm sweating I'm just sitting here and coding and it's not pretty hard here the stuff you're doing so it's not because of the code I'm sweating It's really because of the heat today and we've got nine o'clock yeah it's crazy anyway so here we got the name and then let's say we have to yeah that's it right so we had to manually do that stuff for every single dto for every single superhero and we don't want to do that but what we can do is we can use automapa right so this is where automepper comes in and with Auto mapper this is done kinda automatically we still have to configure some stuff but it as soon as you configure that it works out of the box and this is really great so let's install auto mapper next just right click the or let's stop the application first just close the terminal here and then we manage or when you get packages and browse and look for automobile it's not this one well you think you can also use this one but the only thing we need is this here automatic extensions for asp.net core so Auto mapper extensions Microsoft dependency injection that's the one we need and when this is installed we have to configure it in the in the program CS let's just double check here in our project file there it is right so this is automobile now version currently is 11. swashbuckle is for Swagger UI also a nice name and you know in the program CS we have to configure or register autumnapper or edit so that would be Builder services and then add Auto mapper right but this is not everything we have to add an argument here and this is an assembly and we can simply do something like that just the assembly here and close the parenthesis and as you can see now the parameter is an array of assemblies in essence but we can just use this one the single one and that's it so this is the the first thing we have to do and now I would say we just try this out right so back to the controller and now we try to map our entities so the superhero entities to the superhero dto but the first thing we need is we have to inject our mapper first so let's add one private read only and then this is I mappa from Auto mapper I call this mapper and then we create an a new Constructor generate Constructor so what I did is well I pressed Ctrl period and then we choose generate Constructor that's what I need and now here regarding the heroes first we have to return the action result and now we iterate through all the heroes we do that with a little link statement here select and then for every hero we say mepa please map our hero to the superhero dto alright so this is already the destination and the source then is every single hero in our list of Heroes right so the result you would expect now is just um a list of only of the heroes but only with the name first name last name and the place just double check here this is the superhero dto name first name last name and the place and when we have another kid already rebuilt but uh I think you uh you get the idea right we can again do something like that stop this and run the app again there it is already refresh and here we get all our heroes with all the data but now we just want the yeah we rebuilt it any time we change something I was just confused by the error message here and now let's try that again let's just just make sure to restart the application there it is just reload here because sometimes hot reload you know doesn't always work and now pay attention we hit execute we're getting an error and this is okay because it says missing type map configuration or unsupported mapping so it's called Auto mapper but it doesn't work automatically out of the box we still have to configure one thing but then I promise it will work and what we have to do is we have to add a profile and then create a map for our mappings all right so many mappings so what we need now is another class so we add one and let's just call this Auto mapper profile and this one and here it's from profile provides a name configuration for maps naming conventions become scope per profile all right and in here now we add almost we add a Constructor and now we just write create map and now we start with the source and then the destination all right so from our super hero we want to map to the super hero dto save that don't know why the arrow comes it's the latest version of Visual Studio code uh not code Visual Studio 2022 but let's just close this and run that one more time there's still an error is there really another process not sure about that let's just let's just rebuild the complete solution succeed it and now I hope that you're getting here so let's just try this one more time I hit execute and voila this is the result now we only get the name the first thing the last name and the place great and of course this also works the other way around so now if we want to create a superhero and we want to use the superhero dto for the request what we can do is we can just write another method here and since I'm a really lazy coder let's just copy this make a post method out of that and let's call this add just one hero and this now gets a superhero dto which is our new hero all right and now in here well we could do it in one landed but just let's just do it like that we have a new hero here and use mapa map that's almost correct we wanna map the new hero not into the dto it already is a dto into a super to a super hero entity and now here we say Heroes add hero because again we cannot use our new hero here of course it's another type of list so let's just say this is now a hero and we add it and then again we just select all of them and map them back to superhero dtos so let's save this now Visual Studio Jesus Christ what is going on here so let's stop the app again and save everything I thought it was already stopped I don't know why this is happening but still let's just run this and let's have another okay here's now our post method and what's happening now and let's just use this body we're getting another error of course we have to configure the mapping again all right so just the other way around so here now you can again just copy this paste it here or now we want to map the dto to a superhero stop everything but we also have to stop the the terminal here run it and now let's just refresh the page again drum roll please but let's use a great name here almost this is now the Batman Bruce Wayne and a Gotham City and we hit execute and it works so the superhero was added with the help of the superhero DDO and then it is also returned as a superhero dto and that's everything in essence so this is a really short tutorial for now as always you can get the code uh from my GitHub repository of course check out the link in the video description but that's it now for a little quick and simple introduction to Auto mapper yeah that's it it is late half past nine and still really hot in here but I hoped you learned something with automapa and data transfer objects in this little quick tutorial if so please click the like button and maybe even subscribe to my channel it means the world to me thank you so much and maybe again the newsletter is something for you in particular for the upcoming.net web development bootcamp if you're subscribed to that newsletter you will be the first to know when this thing is available and that's it now I am sweating I already told you that so I will get a shower now make again today because it is it's just crazy anyways if you want more.net tutorials then please check out the ones here on the side or just go to my channel here on YouTube and look for other stuff thank you very much for checking out these videos and also thank you very much for watching this tutorial here so thank you very much for your time and I hope I see you next time take care
Info
Channel: Patrick God
Views: 51,093
Rating: undefined out of 5
Keywords:
Id: AxHER2SlIjg
Channel Id: undefined
Length: 15min 47sec (947 seconds)
Published: Tue Aug 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.