.NET 8 🔥🚀 : Guide to AutoMapper - Simplify Your Object Mapping

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends thank you for watching this video I am Muhammad and today we're going to be discussing how we can actually start utilizing automapper inside ournet application we're going to be going through the setup a process and how we can actually utilize it inside our controllers or anywhere we need to so let's get started so first of all I'm going to be creating a new net web API so I'm going to new web API Das controllers Das name I'm going to call it auto API okay perfect so now all I'm going to do is I'm going to open this in Rider so now that my application has open in all I need to do is going to some packages so I'm going to open my terminal and I'm going to add the following net add package automapper okay perfect and the second one is going to be let's clear this up and let's make this a bit higher as well okay do nut add package automapper do extensions. microsoft. dependency injection okay perfect so now that has been installed successfully all I'm going to do is just verify the installation by going to edit and edit CS Pro if if we go down we can see I have Auto maper available and I have my dependency injection available okay perfect so the next step that what I need to do is I need to actually configure my program.cs to actually utilize automapper and this is going to be fairly straightforward so all I'm going to be doing here is Builder do services. add automapper and all I'm going to be doing here is I'm going to be basically telling automapper which dll file that it needs to scan in order for it to find the mapping that it needs to do in order for it to work and the reason I'm doing this is right now my application is formed of one single DL but what happen if my application has multiple class libraries what we need to do is we need to allow my automa in order for it to scan those DLS in order for it to find the mapping that it needs to and that's what we're doing here so here all I'm going to be doing is appdomain do current domain. getet assemblies and basically the get assemblies here will allow us to scan all of the DLS and load them in order for automa to figure to figure them out and find all of the mappings that we need to okay perfect so once we have done that the next step is inside my rot directory I'm going to create a new folder and inside this folder I'm going to be calling it profiles and the reason I created profile is because automa relies on profiling entities together in order for it to work so what does this mean so inside every single mapping so if I want to map a dto to a class which which we're going to be seeing how we're going to be doing this right now it's going to have to have some kind of a profile so automa will be able to detect it and order for us to make all of the mappings together all I'm going to do here is I'm going to first of all before we add this profile let us add a model and this model here we're going to have inside of it one single directory and I'm just going to make a DB set so we're going to be creating a user class here and for this user we might have let's say prop we're going to have a good so for example full ID we're going to have another prop for let's say string first name let's put equal string. mty and then I'm going to have another one for uh last name we're going to have one for email and I think that should be it actually let's add one more for example let's add something which a bit more complex let's say Street say address or post code and let's add Street to it something like that okay perfect so now I have my user which going to be stored somehow in this database we're not going to be going to the database setup or we're going to be seeing of how we can actually transfer this object types and what I'm going to also add is another directory inside my models I'm going to call it dtos and within this responses we want to return the address of the or for example that user so I'm going to put here user response something like that get user response and this is going to be more uh streamlined so I'm going to put a string I'm going to put full name rather than first name last name and I'm going to have address and I don't want to return the ID and I want to return the email okay perfect so now instead of having to send all of this back all I want to do is I'm going to return a more streamlined approach which has full name address and email so what I want to do is I want to wait where actually convert this object the dynamically in order for it to be returned as this so how would this work now we need to create a profile for this combination in order for it to work so all I'm going to do here inside the profile I'm going to create a new class and I'm going to call this class user profile okay perfect so inside the user profile the first thing that I need to do is I need to inherit from the profiles profile in the automapper and this will allow basically this class to be identified when automapper is actually scanning the D in order for it to find this class which going to have the contain which going to contain the mapping that's need for autom to work perfect so now in order for me to utilize it what I need to do is inside my Constructor of this class user profile and I'm going to utilize something called the create map and this create map is going to tell me between two which two objects I want to create the map and the first object is going to be the source of the information and the second object is going to be where the information is going so I'm going to say it's going to be from users to get user response and once I specified this now inside here what I need to do is I need to do the manual mapping one to one between the objects that I need to do and we're going to be seeing how easy it is to do that after I declared this I need to put dot I'm going to put four member and this here going to allow me to specify the mapping for every single individual item so I'm going to specify the destination just put on the new line destination I'm going to put destination Dot and here it's going to tell me which item do you want to fill and I'm going to say I want to put my full name and then once I do that I need to put a comma and then I need to specify my source and it's going to ask me where is it going to Source com come from I'm going to say Source do map from map from X X do first name plus X do oops dot last name we forgot the plus here and last name okay perfect let's make this in a single line so we can actually see it better so what are we doing here so basically after I told automapper what are the two objects that I want to map to I'm specifying that for every single member that I want to map in this case I'm taking I want to fill the information of full name I'm telling it that it needs to go to the source it needs to map from the first name and the last name so as simple as that so now let's continue so this is not finished yet I'm going to put do for member and I'm going to specify the destination and the destination is going to be the email and for the email we're not going to be really doing any manipulation so or we can actually do some custom or local manipulation so we can put source and I can say SRC do map from let's put this on a new line so we're actually able to see it and then here all I'm going to do is x x. email and for example I can say here something like this I can put dollar sign and let's take this and put here the can say email something like that so you can see I have different types of flexibility when it comes to actually changing the outcome and the last one that I'm going to add is for the address I'm going to put again for member I'm going to specify the destination and the destination is going to be the address and then once I have done that I'm going to spe specify the source similar to what we have before let's put this on the new line SRC do map from and here I'm going to specify x and x do the address so I'm going to put first let us use string interpolation so we can make it a bit nicer going to say user address and then I'm going to first of all the post code and then I'm going to specify the other attribute so X do street address okay perfect so what I done here is very simply I basically declared my mapping I declared to Which object to Which object is going to be mapped once I have done that I've basically for every single MPP I actually defined this mapping processor okay perfect and we can see here it's pretty straightforward so what I'm going to do here is pretty straightforward I'm just going to get a new controller and I'm going to call it user users controller let's take the same structure of this and let's take all of this and then we can clear it up later okay perfect so here this is going to be users controllers users controllers users controllers I don't need this and that's basically it so all I'm going to do right now here is I'm going to create a post request and we're going to and sorry a get request and we're going to be seeing how we can actually transfer this object through a get request so we can see that data transformation so we're going to put HTTP G and we're going to put public async actually we don't need need the async so we can put public void actually public I action result and we're going to say get user again this is all like a demo endpoint and we're going to be basically returning an okay here and now we need to create this okay object that we want to return so what I'm going to do is I'm going to declare a new variable so I'm going to put user underscore user equal new user let's fix the references okay perfect let's make it far and here all I'm going to be doing is the email we're going to put Muhammad dus.com so imagine let's imagine this is a user coming from the database and we can put here the first name is going to going to be Muhammad the last name which is going to be L the ID we're just going to generate a random goid so I'm going to put goid do new goid perfect post code I'm just going to put a random post code let's say uh l122 something like that and last one it's going to street name I'm going to put beut street so now basically what I have here is I have my user class How can I convert this user class that I have for the cic example we can say it's cing from the database to the gut user response pretty straightforward and very easy to do the first thing that needs to be done here is I need to insect automapper into my into my controller so I'm going to put private read only IM mapper and I'm going to call it mapper perfect now I need to inject it through the Constructor so let's do this perfect now we can see it's injected through the Constructor I have my logger and I have my mapper once I have done this so I'm going to utilize this one here so I'm going to put V return user I can call it whatever I want equal underscore mapper do map and here it's going to take what type of object that I want to up to and in this case going to be the get user response and it's going to take the underscore user and all I'm going to be doing here is for my return rer I'm going to put this here for the okay so I can actually see it so now if I run this and open up in my browser perfect if I click on users and I'm going to click on write out click on execute now we can see I was able to get my full users in the same format that I want so basically I don't have the use the first name and last name separated I have the address in the structure that I want and I have the email in the structure that I want and all of that has happened within the autom maper perfect so now now let's do one more example and this is for example if I'm trying to create a user and the user is being provided for me from the uh user itself so so instead of here having a response I'm going to create a new request a new directory I'm going to call it requests and this is going to be pretty straightforward I'm going to create a new another one I'm going to say post user or create user request great so I'm just going to copy paste some of these items so I'm just going to take first name last name and this for my create user and basically what I want to do here is I want to inject this convert this into a user so for this user here I'm just going to add a couple of more item so I'm going to add prop prop say string actually date time uh created at which we're going to generate automatically now we're going to put prop uh int and it's going to be the status of this user something like that so what I want to do right now is I'm going to take the first name that the user is going to provide and convert them into this object directly whenever a user want to register so what I'm going to do here I'm going to create a new request so I'm going to put HTTP post and public I action result post actually going see called create user we're going to be taking from body the create user request going to call it user and all I'm going to do here in order for me to save it I'm going to say VAR new user equal mapper do map I'm going to be converting this to a user so let's do this right now so user and it's going to take the user here so once I have done this all I'm going to be doing is just return the new user again it is all uh for demo purposes you should not do this in real life scenario so I'm going to put here new user okay great so once I have done all of that now I need to create the mapping from this create user to my users so again going back to the user profile let's make this a bit longer here and I'm going to create a new mapping so I'm going to say create map which is going to be from create user request to user and the information that I really want to actually populate is going to be the for member it's going to be the ID so I'm going to put destination let's put it on a new line destination destination. ID it's going to be the first one and the source is going to be SRC do map from and all I'm going to be doing here is x x. goid actually going to put goid do new goid so as soon as I do the mapping I'm going to be creating a new goid for this so that's going to be uh the first one the the second one that I want to add is going to be for member and the destination is going to be the status and I'm going to say the source is SRC equal on the new line SRC do map from and here I'm going to say x I'm going to say one because that's going to be the value that I want to give to it like one is active zero is deleted for example and lastly date added for member and again destination destination. dat added do created date actually created ad and here what I'm going to say is SRC do actually it's going to be destination SRC put it on a new line SRC do map from X I'm going to say here date time. now. UTC actually UTC now and that's it so here what I'm doing is we're having the create user and we have the user but you might think to yourself okay we did not really map the rest of them so we have first first name last name email postcode I did not map any of that and the reason why I did not map any of that is automapper is actually smart enough to realize that these are the same value because they have the same uh property names and it will automatically match them for me so I don't really have to do that work so now let's run this and let's test it out so let's go to my web browser I'm going to refresh this and we can see now I have a post try it out I'm going to put my first name Muhammad last name email Muhammad test.com post code let's say L1 1 2 3 4 five and street is going to be by ro street so now let's see what's going to happen now click on execute and we can see I got back my information with the newly created date with the ID that I set for it to generate with the status that I have provided as well with the mapping for all of the other values that currently exist inside my object so this basically video give you a jump start on how to utilize automapper inside your application I hope it the video was helpful if you have any question as always please make sure you put them in the comments down below and if you'd like to support me please consider supporting me on patreon or buying me a coffee with that said thank you very much for watching and have a great day
Info
Channel: Mohamad Lawand
Views: 8,326
Rating: undefined out of 5
Keywords: beginner guide, step by step, csharp, c#, dotnet, di, Coding Tutorial, Advanced DotNet, Backend Development, Code Optimization, Developer Guide, .net 8, dotnet 8, Coding Best Practices, Programming Best Practices, ASP.NET Core, Role-Based Authorization, .NET Developer, AutoMapper tutorial, Object mapping in .NET, C# AutoMapper guide, Data mapping techniques, Efficient coding in .NET, C# data transfer objects, c# dto, Code refactoring with AutoMapper, Automapper configuration
Id: 87fhsf8gfDg
Channel Id: undefined
Length: 15min 4sec (904 seconds)
Published: Mon Jan 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.