.NET 6 - AutoMapper & Data Transfer Objects (DTOs) πŸ—Ί

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign thank you for watching this video I am Muhammad and today we're gonna be discussing automaper with an hour.net6 web API we're going to be seeing how we can actually utilize it in order for us to transform objects from one pack to another we're going to be seeing how we can actually make it easy for anyone who's trying to consume our apis and basically have a much more better response type when for every single request which is coming into our API if you like this video please like share and subscribe it will really helped the channel as well if you'd like to support me please consider supporting me on patreon or buying me a coffee now grab your cup of coffee and let's get started so first of all what is automaper automamper is simply a library that allows us to transfer one object to another as simple as that it will take in one object and it will basically produce a different object and we can configure the way that we want to transform these objects any way we want in today's video what we're gonna be doing first is we're going to be creating an application where can I be installing the automobile packages and then we're going to be creating a controller within that controller what we're going to be doing is we're going to be having all of the actions inside those controllers without the automapet implementation and then we're going to be seeing how once we Implement automobile how the Simplicity of our API is going to be there as well as how we can actually have a much more better API endpoints basically because we have our much more concise objects that we can actually utilize so first of all let's get started by creating our application so inside our terminal what we're going to be doing is we're going to be creating a new web API and this web API is going to be.net new web API on and I'm gonna call it mapper up you can call it whatever we want perfect now that the application has been created successfully the second thing is what I'm going to be doing is I'm going to open it in the rider now that automaper is open let's see our application so we can see it here let's open up program.cs perfect everything looks fine now let's open our terminal and inside our terminal I'm just going to do the nut build to make sure everything is building as it should be perfect as our application build so the first step as we have said we need to install the packages and these are going to be the automobile packages that's going to allow us to utilize automap it within our application so to do that let's remove this we're gonna put dot not add packages package actually automaper this is the first package perfect I know the second package is gonna be a package that will allow us to actually utilize automapper within.net which is going to be allow us to inject it within our application so it's going to be a dependency induction package for automaper but not add package automaper for the extensions dot Microsoft dot dependency injection perfect now if we go to our CS approach we can see here that I have my automapper installed as well as my dependency instruction for automapers already installed perfect so once we have done that because basically we're going to be dealing with objects the first thing what we're going to be doing is we're going to be creating a folder called models we're going to inside this models folder we're going to be having uh the first basically database table representation we're going to be utilizing a driver table and for the sake of this video we're not going to be connecting to an actual database what we're going to be doing is we're going to be utilizing an in the memory database just to make this video a bit quick I know it's not going to be a few hours video so first of all we're going to be creating the model and then we're going to be seeing how we can create our controller and then basically from there we'll see how we can actually Implement all of the automobile stuff that we want so in the root directory we're going to be creating a new directory and we're going to call it models and inside these models for what we're going to be doing we're going to be creating a new class and this class we're going to be calling a driver perfect and inside the strawberry class we're gonna have first of all a good as the ID so that's the first thing then we're gonna have string as first name can have a string as last name we're gonna have an integer as a driver number we're gonna have a date added so it's gonna be date time I was gonna say here date added we're gonna have another one as well date time date updated foreign actually not daytime here we're gonna just put integer status and lastly we're gonna put in World Championship okay so basically this is a very simple model where basically we have a driver and we have different specification uh properties for that driver so once we have done that the next step for us is we're going to be creating the controller and inside our controllers folder we're gonna basically date add C controller and we're gonna call the drivers controller perfect inside this driver's controller what we're going to be doing is let's take whatever we have here and build on top of it so let's take this we're going to make it here let's take all of this all the way to here we don't need any more than that and then don't need this we don't need this okay so now this one needs to be driver's controller this one here this one here and we don't need this okay so now we have a very basic and bare bone controller So within this basic and bearable controller what we're gonna be doing is we're going to be adding some endpoints and these endpoints basically they're going to be representing the crowd operation that we can do on a controller once we add them we're going to be seeing how we can actually communicate with these endpoints without utilizing automaper then we're gonna be basically adding the automaper configuration and we're gonna see how that's gonna change so first things first because also uh we're gonna be because also it could be said as we said we're going to be utilizing an in-memory database so we're just going to be creating a list of drivers on top of this controller and using it as our inventory database for now so let's do this right now it's going to be private static list of drivers I'm gonna say drivers equal new oops and let's just fix those references perfect so now the first endpoint we're going to be doing is I'm gonna say get all drivers and here we're just gonna put public I action resolved get drivers and basically what we're going to be doing here we're going to put VAR s equal drivers dot oops drivers dot where I just want to get whatever driver are active so state is equal to one and we're gonna make it to list so I see why is this not happy oh I'm gonna say here all drivers and it needs to be equal to equal to one and then here all I'm gonna say is return okay with all drivers so that's gonna be our first endpoint the second one it's gonna be basically the other one is going to be adding a driver so it's going to be HTTP post when that's going to be public I action resolved create driver and it's going to take a driver I'm gonna call it data and let's not create it's create so let's see and here all I'm gonna say in F model state DOT is valid do some sample validation and then we'll have drivers dot add data I'm gonna return create created that action I'm gonna say get driver which is something we have not created yet I'm just gonna pass new data.id which is the ID that has been generated for it and just kind of pass the data very simple and in case something went wrong I'm just gonna return new Json results and I'm gonna say something went wrong and I'm gonna pass a HTTP status code http status or it's going to be I think status code yeah equal 500. so that's in case something goes wrong and we need to send back so that's something actually just do it like this okay so once we have done that actually let's give it back as it was easier to eat for now now let's do the get driver one basically cut driver is going to cut as a single driver and we're gonna put public actually yeah public uh let's put HTTP got and this one's going to take an ID because we're pressing on ID and we're going to say public I action result get driver it's going to take a good as an ID I'm gonna say VAR item equal drivers.first or default x dot ID equal equal ID very simple and what we're going to be doing if item equal equal model I'm gonna return not found elsewhere gonna be returning okay with the item and let's add a couple more so let's add the put statement for example http put and we're gonna Also let's pass the ID for it template actually I can just do ID it's going to be public I actually resolved update driver and it's gonna take a good for the ID as well as the driver data and all we need to do first of all if the idea that you are passing in the URL is different than driver.id okay it's going to be data.id in case it's different we're gonna be returning bad request so far everything here we're doing is a simple Cloud operation on a controller very simple straight to the point we're not really adding anything in you so it's just in particular adding updating deleting items to our uh driver table that's all so here let's put our existing driver for example equal drivers DOT first or default and we're going to match them on an ID equal equal data dot ID and if existing driver equal equal null return not found else but we're going to put existing driver dot for example let's say driver number we're gonna say data dot driver number existing driver.firstname equal data.firstname existingdriver.lastname equal data.lastname existingdriver Dot World Championships equal data.world championships okay and once we have done that we're just gonna return a new content and that will tell us basically that the update has been completed successfully so I think that's a good uh implementation maybe we'll add a delete later on but I think for now that should be okay let's add a delete plus since we already added everything why not added delete so let's put again it's going to be http got also ID and public all right action result and we're gonna make a delete driver all it's going to take is to go with an ID and here once we have that the next thing it's gonna be uh for existing driver equal drivers DOT first or default and all we're doing is ID equal equal ID existing exist existing driver okay so once we have this existing driver here so once we have done that the next one we're gonna say if existing driver is not equal to none actually if it's equal to none we're gonna return not fund and what we're going to be doing is we're going to say existing driver that the status equals zero which means that we have deleted that and then we're gonna be returning uh we can return it similar so let's just existing driver or we can return no content obviously return no content okay so basically right now this will summarize the full crowd operation so what we're done here is we have created a get all answered update and delete and basically within all of that what we are doing is a and as well get by ID so once we have created the secret operations what we're going to be doing that now we're going to be experimenting with it once we actually make sure it's working as it should be the next step is we're going to be implementing automobile and see how we can actually enhance this API by actually utilizing automaper for the objects so now let's run this okay we can have here that is not happy with something so we can see that driver explicit method so let's see that okay let's make this as HTTP get and let's try this again and it's telling us that get drivers by ID where it starts okay needs to be unique so what we can do here is we can put route oops route and we can see here you got driver let's try this now still complaining so we can see that drivers action required a unique path okay uh we can see here that we have our gut driver and let's see okay so what we need to do is we need to make this instead of I gotta delete apologies and let's try this now okay perfect so let's take the C so now that we can see that we have all of our endpoints available so we can see here that now it will not do anything because we don't have any drivers you can see it's an empty array let's try to adding a single new driver so let's try it out let's keep the same good first name I'm gonna put myself last name number let's say 144 status One World Championship zero so let's execute this and we got a confirmation with the 201 and now if I execute all we can see that I got my list of the driver perfect let's add another one let's change let's go with with this S7 and here we're gonna put Lewis uh we're gonna say here Hamilton driver number 44 status One World Championship seven but it should be eight and a spotier and let's execute and here we can see that we have now two and let's do the third one actually that should be more than enough so now that we have these uh and you can see here first so the first uh thing that you want to discuss when it comes to this if we take a look at the actual body we can see here that when we're returning we're returning the status date updated date added we're returning uh first name and last name and completely different fields as well when we're creating it basically we're passing the ID which we shouldn't because basically this should be something that the API will need to Auto generate the date added date updated is something that we should not be doing is the API also which needs to be taken care of that as well as the status So within automaper let's see how we can actually create an object actually let's see first how we can simplify this and then we can see how we can utilize Auto mapper so I'm going to keep this tab open and I'm gonna put the new one here so we can actually compare everything a new version that we're doing so if we come here and let's go go back to Rider and we stopped this so first things first is when I'm creating a new driver I don't want basically to have all of this information available so basically how well is it if I go to a driver I don't want to add all of this all I want to add is first name last name driver number and dual Championship I wanted us to be taken care of by the API itself I want to make it as simple as possible for anyone who's consuming my API to just send a simple information and then basically the API will take care of the rest of it so to do that and what I'm going to be doing is I'm gonna go back to the application and inside the root directly what I'm going to be doing is I'm going to be creating a new folder actually inside the models I'm going to be creating a new directory I'm gonna call this dto dtos and inside this DDOS I'm gonna say create a new directory again and I'm gonna say this incoming dto actually just incoming and we're gonna say outgoing okay so the incoming dto basically again every single request which is coming in the old application and edto which is done for the data transfer object which basically is means an object that the client of the API will be sending to us so any incoming or any object which is the client can assign to us is going to go into the incoming details and any object that you are sending back it's gonna be basically also uh in the outgoing GTO so incoming for everything coming in and outgoing is going to be for everything that the API will generate and actually the my client will receive so inside the incoming dto the first dto that I want to create or data transfer object is going to be driver for creation dto and inside of this as I mentioned all I want to have send is the first name and then I'm gonna put the last name and I want the driver number which is going to be an end and then lastly what I want to add is their world championship World Championships okay so now that I have that champion oh championships within us okay so once I have done that let's take this and let's go back to our driver's controller and here instead of creating a driver in this way uh what I'm going to be doing is I'm gonna update it so instead of utilizing the entire driver uh object which is basically my database table reference I'm going to basically utilize the driver for creation.to and then I'm gonna do the map into the driver object so let's do this here it's gonna be driver for creation that's fix those references and here we can see first start complaining so how do we fix that it's very simple we're gonna put VAR underscore driver you equally new new driver and let's fill the information from here so what do we what do we have here we have an ID this ID needs to be Auto generated so we're going to put a new code so this way first of all the API will take care of this the second status is going to be one because the API is going to take care of it date added again date updated just keep it the same date time and then we have driver number and this driver number is going to come from the data object and then we're gonna have the first name it's gonna also come from the data object Dot first name we're gonna have the last name and we're gonna also have the world championship okay so now what we have here is basically we have created a new object which is called driver we have manually mapped everything from the uh from the dto incoming dto which is driver for creation to this object and once we have mapped this out what we're doing right now is we're going to visualize this a new object in order for us to save it in the list so right now if I just take underscore driver and store it here and instead of returning data I'm going to be returning that driver which is something else that we need to take a look at later on so now if I do this and let's run this see how it's gonna change my API so now that it's running let's go back into our web browser and I'm just going to refresh this so here we can see this is the initial dto to create an object actually this was the original body request and now if I come here I click on post we can see that it's way more simpler so this is how it was before where is it see this is how it was before ID first name driver out of information and here we can see it's much more simpler with four endpoint so let's update this so here what first of all let's see because I think this is a static list so we're not gonna have anything here exactly so that we're gonna add the first driver and we're gonna put Lewis and here we're gonna put Hamilton driver number 44. World Championship seven although it should be eight uh execute and we can see here we're still getting this ugly response but that's fine for now and if we take a look here I will execute we'll see it's adding so we are able to see how we can actually simplify our input or basically the object that we are sending to the API and basically we are delegating much more of the work to the API to do instead of having everything being provided by the client and we can see how we can actually doing all of this mapping manually so what we're doing is we're mapping that dto that is incoming from the API to uh incoming incoming from the client sorry to the AP to the object inside the API and we're doing all of this manually and here what we're going to be doing right now we're going to be seeing how auto market is able to help us with this we're going to be creating a set of rules for automaper and automaper will be basically able to take care of all of this mappings for us instead of us doing it manually so let's see how we can actually implement this so let's go back to Rider and inside Rider here inside our root folder I'm going to be creating a new directory and insta first of all let me let us create the code profiles and the reason we created a directory called the profiles because basically here what we're doing is for every single objects that we want to map or basically let's say currently we have the driver object which is basically representing a table in the database for that uh object itself which is the driver we could have multiple mappings we can have for example for creation for update for returning back information to the user so what we're gonna doing inside this profiles we're going to be creating a single profile for every single table in the database and basically from there we'll what we'll be doing is we're going to be creating those mapping inside the single profile and here a profile me is just like an instruction set that we're going to be doing or giving automaper for it to actually know how it will map the object one to another so inside the profiles here what we're going to be doing is we're going to create a new class and we're going to call the driver profile perfect and what we need to do is we need to unhide it from the profile class and basically we can see here it's coming from automapper and then what we're going to be doing is we're going to be creating a Constructor because everything for automapet is going to be coming within the Constructor because once it's constructed we'll actually know how it will map it and then we're going to be basically creating a map we're turning it here that you need to actually start implementing a map between whatever object that we want to give you and the way we do that is automatically provide us with a method called the create map and that create map will basically allow us to do these mappings manually so here what we're going to be saying is we're going to be creating this map for the driver and basically it's gonna be from the driver creation dto driver so basically what we're telling it here is this mapping that we're currently creating is going to be mapping every information uh from an object which is called driver for creation detail that which has created and those data will be need to transformed into the drivers or transforming transforming the object from a driver for creation to driver and we can see here the order is really important because if we switch it up and we put a driver and then we put the driver for creation automaper will think that we're taking information from the driver in the class and we're transforming it to the driver for creation so be very careful here about the order first of all it needs to be whatever the source and then the destination so the source is driver for creation and destination as driver so once we have done that now we can start the actual mapping I will put here dot four member we're gonna be let's fill it up and again we can explain it and we're going to say the destination it's gonna be dust.id and we're gonna see here also the I was always going to be options Dot map from and then here all I'm gonna say Is Us RC SRC can you see it let's see if it's available see it okay as you can see it SRC and we're just gonna be creating a good so we're gonna say hey here just put go with dot a new new Goods and we're just gonna close it okay so let's explain this oh this is adding a lot of okay so what happened okay let's put here a semicolon at the end okay so let's explain what's Happening Here so first of all what we did is let's make this smaller so it will be easier to see uh yes it's my it's better like this so basically what we did is we're creating a map from the source to the destination and we're telling it that for every member and this we need to repeat for every single one of them when you whenever you see that you have an ID that we need to fill what you need to do is you need to create a new guide for it and this here was reflected inside our controller by us creating the good manually so what we're saying is in order for you whenever you want to create this new driver profile the ID for it should be automatically generated do not expect it it's coming from the driveway creation and that's why we can see here that this is my it's within this mapping it's going to be automatically responsible for creating this map for us we're not going to be creating the ID for it automapet is here going to be responsible for generating the ID for us or basically delegating this response District this responsibility for automatic for it to do it for us so that's gonna be the first one so the second one that we're gonna be uh adding let's see it's gonna be dot four member and right now what we're going to be doing is which basically just before we do that you can see here it says destination member and basically number option to the destination number is where do we want to put the information and the member option configuration is what what information do you want to put there just for the clarification so right now what we're going to be doing is just gonna put the first name so destination is gonna be destination.firstname and we can put similarly to what we have done before so it's going to be options and it's going to be options again dot map from and I'm going to put SRC and I'm gonna put here for example SRC oops SRC .firstname and basically here we can see that this automatically got handled by first name for us don't have to do anything else rather than just making sure let's make this on the same line that's making sure that it's mapping from one to another now let's do the same thing for last name so for remember destination destination Dot last name and it's gonna be options dot map from and I'm gonna put SRC SRC dot let's say last name perfect then what I'm going to be doing is for a driver's number or yeah driver's number so instead of me just copying writing it again I'm just gonna copy paste it's way faster and just change the value so it's going to put here driver's number to Driver's number and we're gonna have World Championship World Championships and the last one it's gonna be the status and instead of the status is going to be one because I want to input the value one so basically here we can see it makes let's go a bit higher so here you can see one more so here you can see that I have all of these mappings automatically configured for with an automapper so whenever I'm having those Creations so whenever I'm trying to create a new object all I'm saying is this ID is gonna map to a new ID so and basically for first name I'm taking the first name that has been provided for me from the dto object last name I'm taking from the dto object 12 Championship from the dto object as well as the driver number from the dto everything else is being automatically created for me by the mapper we still we still need to add the data created updated so let's add it right now so here just add them here and here we're gonna put date created or create added it's going to be daytime.now date time dot now and the last one is going to be date updated it's also going to be daytime.now datetime dot now and basically we can see here that once we have specified this configuration all we need to do right now is just update our first of all program.ts so our application will know that automatic is installed and basically it will know that a configuration for automaper already exists so that's going to be the first option the first thing that we need to do then we need to update our controller by injecting automaper inside the Constructor and then actually utilizing it inside our actions so let's see this right now so let's go first to our program.ts and inside my program.ts it's going to be a very simple implementation I'm just going to say Builder dot Services dot add automaper and here all I'm gonna say is I'm gonna up domain dot current domain dot get assemblies and that's it so what are we doing here so basically what we're doing is we are telling our dependency instruction container that you need to take into consideration that automaper is there and basically what automapet is going to be doing right now it's gonna be basically actually scanning all of the DLS that being generated by our.not application and then once it's scanned them it can automatically know that inside this DNR there's going to be these classes and these dtos and it's going to refer back to the profile that we have created in order for it to do the mapping and we can see here that's actually doing some kind of smart work in the background where it's actually trying to figure out what are the dlls that we have and based on that it's going to be doing the mapping that we're going to be utilizing so once we have done that now we can go back to our driver controller and the first thing that we're going to be doing is we're just gonna be mapping the uh adding the mapper into our Constructor so here all I'm gonna put is private not profile private read only it's gonna be Auto mapper actually it's going to be imapper I'm gonna put mapper and then I'm just gonna take this and then I'm gonna inject it here and then it's gonna be underscore mapper equal mapper and now once I have done that now we can see here how we can actually abstract this into something way more simpler so I don't have to do all of this all over again so let me remove this All I'm gonna be doing right now is just having VAR underscore driver which is the object that I was sorry the variable that I want and I'm gonna utilizing the mapper dot map and all I'm saying to the Muppet is you need to create an object of type driver from the data object that you're creating and as if you see here all we have to do is add a single line and that single line basically it will take care of all of the mappings that we have it will take care of actually figuring out which objects needs to go to which objects which configuration is to go to which configuration for example and based on that we are able to utilize it directly so right now let's take this for a test so let's click on debug and let's go back to our web browser and inside our web browser here what we're going to be doing is let us just refresh this and let's create a new driver and I'm just going to create a single driver for now and this one we're gonna be called Lewis uh the last name Hamilton driver number is 44 and World Championship seven although it should be eight and then execute and we can see here that's created successfully and here basically after I click on try it out execute we can see I got back my drivers as they should be perfect so let's do a quick recap on what we have done here so first of all is we have created a profile and this profile here basically we actually were able to map every single object from the incoming uh dto to the actual driver information that we have every single one of them have a specific mapping and the powerful the powerful powerful thing about this is let's say I want to make sure every single uh first name that comes in for any reason I want it to be all in capital so all I need to do to do that is I need just to manipulate this so all I need to do is just say the two upper and now every single input that I'm gonna input into my application is going to be upper let's say I want to add certain styling for example so I can just do something like that I can use three concatenation and inside the string concatenation I can say uh let's put it something like this and I can put for example uh I don't know let me put driver number for example so it could be SRC dot driver number I don't know something like that and let's try it out so all I'm doing here is just manipulating these profiles and with one amplit manipulating this profile I'm able to actually adjust the input to my parameters so if I stop this and I click on debug again and now if I go and uh let's see let's add a new just copy this instead of me trying to retype them again and let's go to post trade out and let me paste this here and click on execute we can see here that the first name has automatically been updated so the driver number now became here and then everything went to Upper uppercase and all of that has happened just because I have I was able to to update uh the mapping to whatever I want basically so this is the first item that we're gonna be covering so the second item here is we're going to be seeing how we can do the opposite so now what we're doing is we are taking parameters from the user and basically creating this object and now if we take a look at the uh response type that we're having so we can see here that the response type this is not really something that the user will see so let's say I don't want to send the first name and last name independently I want to just say name and we're going to have the full driver name I don't want to send the date updated data that I don't want to send the status I just want to make it a simple object where the user will be able to consume or backseat yeah the consumer will be able to consume easily so what I'm going to be doing is I'm going to be creating another dto which is going to be responsible for actually taking the object back and then uh with the format that I want and then I'm going to be utilizing automobile to do the mapping for me so let's do this right now so let's go back to Rider and inside my profile I'm gonna go to the driver profile and let's make this a bit smaller and let's see how we can implement this here so when we took the we created the mapping between the incoming the object and the actual object as we said we put driver dto driver for creation dto first and then we put the driver the object that we wanted there so now inside our outgoing folder here inside our dtos inside the models folder we're going to be adding a new class and I'm just going to call this driver dto just gonna be referring to the detail that I'm gonna be returning and here I'm just going to be returning the full name I'm gonna be returning the the driver oops Drive is gonna be and driver number I'm gonna return I go with which is the ID choose ID and lastly I'm just going to return the world championship okay so now that I have this Champion up here okay so now I have this dto I'm just gonna take this and I'm gonna go inside my driver profile and I'm gonna be creating a new mapping for it so if you go all the way down and here after I finish the first one I'm just gonna be basically creating this new one and in order for me to create it it's gonna be utilizing the create map and here I need to specify from what to what so it's going to be from the driver to driver dto it's going to ask me to fix those references and then inside here I'm just gonna put oops Dot for let's fix this dot four member and all I'm gonna be saying here is the destination it's gonna be the destination.full name and I'm gonna basically oops I'm gonna take the information from the object itself that which is going to be the driver so I'm gonna put options options dot map from oh forget the comma so it's going to be map from All I'm gonna be saying right now here is need to take okay let's see if you can see it perfectly I think it's being hidden okay and let's make this smaller yeah so all we're gonna be saying here is gonna be the uh uh x dot first name and the other one is going to be x dot last name and that's it so here for example we were able to concatenate both of the first name and last name and a single sentence so that's gonna be for the first one and then we're gonna put another one which is going to be looked for member and destination as well it's gonna be destination dot ID is going to be a very simple this one similar to what we had before options it's going to be options dot map from and we're gonna put x dot ID simple as that and then now we're gonna put let's copy this and so we can do it for the rest so it's gonna be a World Championship as well as the driver number so driver number driver number and last one is going to be a world championship and let's put here World Championship and then all I'm gonna see is this and now basically what I did here I was able to create a full map and this pool map here is basically taking from the actual object which is the database and basically from that I'm putting it inside a dto that I'm going to be utilizing to return to the users so now all I need to do is go back to my controller and here for example whenever I'm returning this driver so instead of this so let's take it simply by a simpler than this so uh from here instead of returning all of these drivers All I'm gonna be doing in order for me to utilize it is I'm going to be utilizing that map object that I have and that I'm gonna say okay whatever you have here inside this list transfer me to this type of list list not objects so let's see how this can this will work so here all I'm gonna be saying VAR uh I don't know drivers underscore drivers for example equal drivers actually sorry it's kind of equal map mapper dot map it's gonna take here it's going to return an i innumerable of what of all drivers actually of uh driver dto I'm just going to take an input of all drivers so if you take a look here and then we're gonna return this one if you take a look here basically I'm taking an entire list an entire list of my objects the object that I want to return and I'm just mapping them automatically I'm not creating a for Loop one by one I'm not creating uh for each Loop for example I'm not doing anything all I'm just saying is auto mapper take this list check your profiles if you have something there map it for me and return it as simple as that to a single line instead of creating a for Loop and basically try to map all everything from there so right now if we execute this and let's go back to our Firefox and before we do anything here I think oh well I still have it here opened yes so here this is the original one of How It's actually turning now let's see after we add a couple of one How It's Gonna Change so I'm just gonna execute this again now we have one this one we need to update by the way so but we'll do it step by step so here we can see that we have created one and now if I put try it out and execute we can see that I got the new format which is the ID full name driver name a world championship instead of actually having to have to deal with this full object now let me add another driver so I'm gonna put here George I'm gonna put Russell 63 driver number and World Championship zero and execute and now if I come here and I execute this request we can see it's a much more nicer and consistent uh uh much more nicer and easy to understand uh object that's being returned from my API rather from actually utilizing or basically the big object as it is coming from the database and for example another reason is I don't really want to send all of the information that's coming back because some of them contain sensitive information I don't want to really basically sharing that with the client so that's why here creating ADT object which is data transfer object to return is a much better way to actually utilize it on a much more secure way as well so now what we're going to be doing and the last thing we're going to be implementing is actually instead of returning all of this object here I'm just going to be returning a concise object and to do that let's go back to our Rider and when when we are creating a driver here all I'm gonna be doing is uh instead of actually create returning this all I'm going to be doing is I'm going to just put after this one here our new driver I don't know a new driver for example naming things is kind of is the hardest thing in programming I'm gonna put underscore mapper.map I'm gonna basically return to a driver dto and it's gonna take that new driver that has been created and I'm just gonna put here a new driver and let's run this and see how it will work so now that it's running let's go back to my web browser and now once I create a new driver instead of receiving all of this I should receive a nice concise object so let's execute this again and we can see here that I got a nice concise object at the end so let's do now a quick summary of what we have covered today so the first thing that we have done is basically we have creating a web API we have created a controller and you have created an object which represents a database we have created that in memory database rather than actual database and then what we did is we created all some of the cloud operation uh to create delete update so on so forth so once we have done all of that what we have seen has how we can actually interact with this API we have seen the object that we are actually sending to the API and the object that we are receiving back and we saw that there's a lot of room of implementation of enhancements and there we decided that we're going to be utilizing automaper so from within that we have installed the nuget packages for automapper and then we created a profile which basically stands for like the configuration for every single object that we want to map between two different items so we created a profile for the drivers and then we have created a inside of it a mapping for all of the incoming traffic to create dtos as I thought to create a new object as well we have to create a different mapping for all of the things that the API is going to be returning back to the users and once we have done that we have seen how the API experience has completely changed instead of having a big body of the object that we need to send we have delegated all of this to the API itself and the API is actually taking care of it and actually building it for us I think and rather than the client have to provide all of this information and here we can see although this is a very simple example we can see the level of enhancement and the level of actually performance that we're going to be getting under and the level of how much we are making our API easy to use just behaviorizing by the and utilizing this Library I hope this video has been helpful please like share and subscribe if you like it and if you want to have access to the source code please consider subscribing to my patreon or buying me a coffee and thank you very much for watching and have a great day
Info
Channel: Mohamad Lawand
Views: 14,062
Rating: undefined out of 5
Keywords: .net 6, .net, api, step by step, c#, dependency injection, coding, entity framework c#, entity framework, asp.net core tutorial, api security, json api, .net core, asp.net core, web api, postgresql, postgres, postgresql dotnet core, postgresql dotnet, redis cache asp.net mvc, redis cache manager, dotnet core tutorial, dotnet core web api, redis, dotnet core, dotnet performance, automapper, automapper c#, automapper in asp.net core 6, dotnet dto, api dto, data transfer object
Id: Wm7hN0S-AdU
Channel Id: undefined
Length: 54min 41sec (3281 seconds)
Published: Mon Oct 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.