AutoMapper in asp.net core 6 & Data Transfer Objects (DTOs)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so do you want to implement automaper in your project so this is the right place in next five minutes in simple five steps I will tell you how to implement automaper so Step One is we need to add a new head package manager in our project for automaper so let's go to nuget package manager package manager Solutions and let's search for Auto map and this one we are going to use automapath extension for asp.net core let's select our project and click on install and now let's verify our installation so double click on Project and we can see that automaper has installed successfully and we are done with the step one now step two is two we need to Define automaper so I am 1.net6 so I am defining it in the program.cs file if you are on the dotnet core previous version so you may need to Define that in the startup.cs file but it would be same process only so here I am going to say like build up Dot services Dot 8 automaper and here I am going to say like app domain dot current domain Dot get assemblies so this is our step two so in step 3 I am going to create a class for mapping so I already have this model class but I am going to create one more class for uh for the mapping so I let me create one folder here I am giving name as a dtos and under details I am creating a class employee again I am creating a class employee and let me copy and paste same content for now on this class as well we will change it later but for now let's keep it same so we are done with the step three so step four is creating a mapping profile so for that let me create one more folder and I am giving them as a profiles and under there let's create one class and let's give a name maybe custom profile and we need to inherit this class with the profile class which is under the automaper name space so profile and we can see that it is under automaper and our mapping we can Define in the Constructor of this class so let's create a Constructor first and here I am going to Define mapping I am going to creating okay I am going to create a mapping so let's create map and here we can Define source and destination so for example maybe I will say source is my our model which is I'm getting from database and destination is our dtos which I am going to return to the user so I will say models Dot employee is my source and ttos Dot employee is my destination and I will say Rivers so for example when we are saving it we I want to map it reversely right so because of that I'm adding viewers map also so we are done with the step four now last and final step five is use this configuration so let me open our employees controller and first I need to inject the maple object here so it is I Maple under the automobile name space I will say it is a maple and let's create a read-only property for this field for this and now here we can see that we are returning uh the models employee but we want to return the dtos employees so let me remove this using a statement first and now let's add DDOS using statement so now this class is some details and that is the reason we are getting this cast together so now let's change this two variable result equals to this and now I am going to use this Maple object to map this Model start imply list to dtos dot implied list so I will say like mapper dot map and here I need to tell the destination so destination is my dts.imply so maybe I will say dtoms Dot employee and it should be list of and my destination will be result right and let's return this yeah so we are done with the step five and we have successfully converted our a model implies to reduce employee so let's verify this let me run this and let's see the response through Swagger and we are getting the response as expected but this is a simple case now let me go back and let's make few changes in our dtos so let me stop this and what if I don't want first name last name and I want full name here so let me remove this last name here and what if my models and details property name is not same maybe there I am having date of per dob but here I am having a full form as a date of birth so how we are going to make these changes let's try to understand so if your property name is not same then also it is very easy without a paper just go back to your profile where you have defined your mapping so here we have defined like uh map this employees models dot implies with DTS dot implies here I need to explicitly Define the mapping for the properties which is not similar so let's say for member here I will say my destination member for example full name so full name what I want is I want full name is a first name plus last name right so let's do that I need to map from and here I need to define the source made from the source so source is first name and I will say this and then as well C Dot last name so we have defined first name plus last name equals to full name follow our full name property let's define for date of birth so here in the dtos it is date of birth but in the model it is DOB so let's define that and let's check what else we have you apart from that other fields are same only right so let's go back and save this and let me run and verify if you are getting our full name in date of birth correctly or not so let me explain this and here we can see that that Swagger has updated with the full name and date of birth now let's try it out and let's click on execute and great now we are getting full name is expected and we are getting the correct date of birth also so [Music]
Info
Channel: Tapan Dubey
Views: 1,990
Rating: undefined out of 5
Keywords: what is auto mapper, auto mapper in dot net core 6, how to use auto mapper, how to use auto mapper in list, how to use auto mapper in collections, how to register imapper in services, how to apply condition in auto mapper, automapper in asp.net core, automapper .net core, automapper asp.net core 6, automapper in asp.net core 6 web api, automapper in asp.net core 6, automapper c# asp.net core, automapper c# asp.net core 6, automapper c# net core, automapper net core 6
Id: 7Ss6FSxp3RQ
Channel Id: undefined
Length: 11min 54sec (714 seconds)
Published: Wed Mar 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.