Main

Streamlining Data Transfer: Complete Guide to DTOs with AutoMapper in .NET 8 EFCore Applications.

Are you looking for Coding Mentoring? If you're seeking personalized guidance and mentoring for your coding journey, then get in touch!. I offer expert guidance and support to help you level up your programming skills. Contact me on business.netcodehub@gmail.com to book a session and accelerate your coding career. /*Source codes available*/ https://github.com/Netcode-Hub/DemoDotNet8AutoMapperAndDTOs Choose Between Fluent API / Convention & Navigation Property in .NET 8 EFCore Relationship Mapping. https://youtu.be/ldo7JIU24o8 /*Playlist*/ https://www.youtube.com/playlist?list=PL285LgYq_FoIcAb0hElzmfJsd27qgfzCY /*Follow Netcode-Hub*/ https://netcodehub.blogspot.com https://github.com/Netcode-Hub https://twitter.com/NetcodeHub | Twitter https://web.facebook.com/NetcodeHub | Facebook https://www.linkedin.com/in/netcode-hub-90b188258/ | LinkedIn

Netcode-Hub

7 days ago

he friends and everyone welcome back to  net code H Channel I am Frederick as you know and I'm happy to have you here today I  think this is the time that I have to take it through autoa and D I've been working with  this if you watching my videos I have been using automapa and d2os in many projects and  there is a time for me to just break it down for you to get a concept if you know this  already then this going to be a vision to you isn't it but if you know then I'm  going to cover a session
on autoa and D2 as you know before we Dive Right into  this make sure you have subscribed to this Channel and also don't forget to click on that  notification Bell so you receive an update as soon as I do upload new content you can also  like this video to support this channel grow I do offer coaching or mentoring session to  people who are interested in net Technologies talking about blaz Ser assembly Mar Blazer  hybrid where pii if you're interested check this video description I have an email
there  you can write to me through it okay so the first question here is what is an auto mapper  it's very simple Auto mapper it is an object object toob mapper that simplifies the process  of mapping complex object model to D2 and other simpler objects so automapper can help you to  map complex object to simple object that's a dto then the next question is what is dto d2o  what it actually stands for is data transfer objects d2os now D2 serves as an intermediaries  in the word of software deve
lopment d2o is very crucial in terms of web or software development  then what is D on what can D2 help you do a dtoo is an object that carries data from one part of  the system to another it acts as a container for information so you know that when you're creating  a system maybe for authentication for a product system what you need to do here is you need  a product ID you need some password from the user maybe if it's an authentication system you  need an ID you need a password now if you want
to show user information I believe you never show or  you will not show the password to the user isn't it yes because the password it is hidden it not  be secret it must be kept safely and you don't want to display to anybody so in such case you  have to create a dto to extract the properties that you want to display out of the user this and  many more can be classified as what d2os and I'm mapping a complex object to a simple one that is  know as using the auto mapper let's Dive Right to this
with an a practical aspect for you to get  a concept of this so I'm going to create a web API project from here so make sure you have your  Visual Studio 2022 launched create a project in this and now this I'm going to choose  web API so I'm going to say this says net 8 so that not 8 is going to be an autoa and dets  okay so it's better that I put demo on now when I'm done with this Source could be available  at GitHub so check this video description if you want to grab this to be available in 
there now let's get this project created so this is what we're going to do since we  are not going to um Implement database for now this is just to teach you the concept of  automapper in dto so I don't see the need um to actually get database connection okay but  I've made a video several videos on how to um get your database connected and I implement or  decard in it if you having an issue with it do let me know at the comment section and I'll put  that video out for you okay so this what you'
re going to do let's say we going to have a system  and a model a domain for books in that you're going to have book ID book title book AR the year  and on the price of the book okay so that's the model that you're going to create so I have  a sample model here created already so let's go to uh solution then in here let's let's  create a model for this so let's add as um I want to put it at group it well so let's say  this is model and you know what model are our entities that we need for databa
se model let's  add a class for this and this is going to be book So within this book you know we're  going to have a public property and aside from having this public property we are going  to provide the following as well the details of the property that we need for each book  we need an ID the title The auor the year and the price you know this already isn't  it that's fine now let's once we have this we are going to create maybe our service  okay now this service is going to render a list of
books then we're going to create our  API endpoint to consume this service so let's right cck click on this and now let's add maybe  a new folder as well now this is going to be Services let's add a class  to this and we call this book service so within this book service we going  to have a list of books so let's have a public and this there's going to be list  of book we can add the name or the reference so this is get books okay so within this maybe this what we can do we can first have our a
book down here so we  can be calling these books anytime that we want okay so we having this we have a public and let's  make this as private so private this return a list of books and I we calling it as what books so  we saying is equal to um new book then we are providing this in here we can simplify this to  make it just new you can use the new keyword and and that is all so we need not to use all this  I think let's introduce later on so we can um do all the now so put the video now do it y
ourself  okay yeah so I'm done with mine are you also done okay so here it is asking us to add read only so  yes maybe we can make it as read only so we can read only okay and aside from that what again  can this can be simplified so this equal to we have this so that's a new one yes okay so there  is a list that we are creating so in here we now making it in an array form you see then we want  to just get this books so if I call this method get books we are retrieving the books in here  okay no
w this we are not going to make it read only maybe we can add data to this okay so we have  these books all right what is the next one we want to we can be adding so let's make this as public  then we want to add a book to this so also let's make this as public and now this rains nothing  so this is void then you say add book so this add book we pass in this book model and we say  this is book and in here what we are saying is books do add that we can now add this book you see so we just add a b
ook to this  the list and maybe when you talk about delay two we can also perform that okay but for  now let's maintain the this what we need in here get books and maybe get book by ID  I think that would be okay so let's see we have this also this going to return book  then we say um get book by ID then we're going to provide the book ID in here and within  this we can say that it is returning books do find so find we passing the ID so here  is it going to get us can I convert into to okay so t
o a book then we can use books dots we can use first or default so do ID is equal to this  ID okay so we can um also use this okay so this is get my ID and we can also  delete it well yes we can do that okay so let's let's delete since we have this so delete by ID  we can just make it in this form delete book by ID so inste of the first we're going to cut all  this from here they we're going to say books do remove books do remove then in here we can pass  in this so with the remove we going to s
earch by the ID and now when we have it then we're  going to remove it from here let's see why we have in this we need to return this is going  to be void so we don't need to return okay so this is all that we have in our C within the  list that we have in here okay all right so we have that book service ready now let's go to  uh controller and now let's create a controller for this so we have a weather forecast already  we're going to modify this weather forecast and I'll make it book controlle
r so this is book  controller yes I want to modify this now we have book controller and this is going to be an  API SL book so let's remove everything from here okay then let's create Constructor  for this c r let's passing our book service now let's inject this service cont  period we can inject this and it is ready in here okay so the first one is we want to get  all so HTTP get and you want to get all books so get all books and then here we have a public um task and now this is we need an let
's say let's  provide a list of books so list of book then we can include the reference of the model then  we say get books so get books in here okay now within here we want to return the service so we  can say books is equal to then the book service that we have created do get book method okay  so within this instead of um writing it here we can just return okay with this and we can use  this Lambda expression okay okay then we can now call this an AR 10 so you see what you're  doing we are jus
t simplifying this then we going to remove this in here and now let's see  so once you have here this must be an action result so we have this action result  in here then we can pass in our list of books and in there we are returning our  book service. getet books and here what is it saying since we are not getting this  from a database it is not tasked so we can remove the task from here and it should  be action um results so we have get books we can make this as null okay now let's see  get by
ID so let's make it duplicate of this and we can copy this one and on top  here we need to specify only a book so a book in here then we can specify an ID  for this okay so once you have this ID get book and we have to pass in the same ID  and now get book by ID we can now pass in by ID then passing this ID parameter  that is it okay then you're going to have delete so with the delete you're  also going to have the same end point and this will be delete you see so we passing  an ID and on the s
ame so delete book then we can have delayed book by ID okay and we  passing the ID in here and this is not returning anything right so this is an action result so you're not returning  anything you're passing the ID to this so this says this going to not it's going to return empty we have to open a body  for this then we have to call the service we call this service then  at the end we can return okay delete it okay then we have ADD so we have to  call same and now that's going to be the post so
instead of having delete  this is going to be post so we have book okay so add book then add book this will  be the adbook and now the payload that we are accepting here it is book and now book so with  this book object you can now have access to add book end point then from here we can just  pass in this then we say add it so this is how it's going to work okay so defaultly when  you create any system default way this how it's going to be done isn't it yes here we haven't  implemented dtos we
haven't implemented Auto to mapper this a default one let's try it but  before we do that let's register the service in the program. Cs so we can duplicate  this then say add scop so we have book service now this is coming from the folder  names space let's run this and I'll check it out okay so the end points are ready now let's try  to get all books so try this out execute this now let's check this out so you can see that  we have all the the details in here right from ID 1 up to ID 10 so we h
ave book ID book title  author year and on price that's fine let's try to add one one data or one book so we add this  book from here try this out you want to have an ID of 11 and let's execute this it hasn't added  so let's check this out or let's get only ID 11 book execute this yeah and I can see we have it  we can also um delete this try this out passing 11 delete it and it is deleted when we click on  get all books we must see ID one up to 10 that's fine okay but the next question is you kn
ow when  adding data when adding books we don't want to be typing book ID we want the book ID to be sve or  autogenerated you know it's an increment of one one one one one isn't it so can we have a way that  we can implement this automatically so that the user uh will not be able or has shouldn't be able  to type in an ID the only thing that the going to provide here it is the title The Al the year  and the price is it possible yes it is that is where the d2o comes in so D2 is a data transfer  o
bject and it is used to communicate data from two entities so here from our data service our  service um rppo onto a user UI okay so from you between user and under repo or the service so  within this let's see how to implement this we must create a class and on this class you're  going to maintain only these four properties you're going to exclude the ID because the ID  is going to be self-generated so the user do not need to see the ID of the book when adding he  has to see only title auor yea
r and price so that he can provide the values for each and we add the  ID automatically so let's see how to implement it using this dto so we're going to create a folder  in here and now let's before we have to stop this let's create this folder now let's  name this as dto so with this dto we know in D we have to we have a request and a  response request is to add and a response to receive from the server okay so we're  going to create a request dto this is um request this request dto now this r
equest GTO we  need only let's go to the book model and now here we you're going to grab this only title answer  year and price going to exclude the ID we go to the request then in here let's paste this okay  now when we paste this we have to make changes so we're going to copy this we go to our book  service and now where we have this add instead of having this book we have to S it to what  request dto let's add the name space to this so request dto and now here we say it is request  dto but if
I do this check see something if I add this it won't get added why see cannot convert  from um request GTO to books you know we have two models here and you want to convert one to  the other why is not possible that is where the automapper comes in so we are going to map these  two entities request d2o has about four properties whil book has five properties so when you Auto  map it is going to fill up the properties that it says in the request dto is going to fill up  to the the original dto th
at is a book dto not the book detail the book model you see the  book model has five properties and you bear with me that you and I created it together it  has ID title Al and Etc and now the request D2 has only title AR year and price so when you  use autoa you're going to map this four to this four now this is how this auto power works it's  going to check uh loop through this whole model and I'll check does the property that it has in  it correspond with each here if it correspond then the va
lue that the property has is going  to assign to this so title to title author to author year to year price to price but when  you get to ID we don't have any property in the request2 called as an ID so it's going  to skipe that so the property that you could not find is going to skipe it's going to set  as it's going to skipe that one okay so you want to map the title the other the year and the  price to this book model that's the reason why we need to use automapper here so autoa goes  with re
quest it goes with D2 automapper goes with d2os or d2os goes with what automapper  so this are supposed to be done now how do we Implement autoa in this it's a package that we  can install so quickly let's go to the solution raet dependencies and I go to management get  packages in this you're going to install autoa from Jimmy Bogard I think so so click on  the browse top and I in here let's install autoa so we want to install this autoa  extension that's Microsoft dependency injection by jimm B
ard so this is what we  need to install Okay so if it is saying that this package was duplicated use  automa instead okay so we can use the original one here because everything  is part of this we can still inject this anywhere so we let's install this 8.1  let's click this package and I'll get this installed okay so this package has been  installed if you want to verify go to solution dependencies and now packages you  can see we have this autoa in there so once you're done the next thing that
we need to  do here is to make a some configuration we have to register this you go to the program.  Cs file and then in here let's register this Auto mapper so dot then we're going to have  add automapper app domain. Current domain. getet assemblies okay it's going to get all  assemblies that we can use for the profile that we're going to create going to get all of them  and it's going to utilize them in mapping okay the next thing to do here is we have to create  mappa profile so let's create
this it's a very simple you so the package reg this create M  profile and off you go so let's say this is profiles and now in this folder we're going to  create profile so let's create a class and the name here is going to be profile this profile  should supposed to be profile this should be let's make it auto maper profile that's the  right thing to do because it has an inbuilt class known as profile that's we're going to to  inherit from so we don't need to make it profile again that's going t
o be an ambiguous so TOA  profile and this should inherit from profile as I said this is an inbuilt class that is  has to inherit from okay aside from that we need to create a Constructor for this and now  within this Constructor that is where we're going to perform all the magic so first of all  we're going to create so you can see create map and now with this map take note it has from  what the destination so it has t source and a destination can you read from this source is  where the current
contest that you want to map now destination is which one you want to map  to so you want to map from our DTS so request dto then that is our our source and  our destination here it is what book okay so this is the book model so this what we are doing so create we  are mapping from request dto that's the the source and then there's a destination  so maybe I can put something here for you this is source and that is destination okay so when  you come here after creating this you go to your servic
e in here book service now  let's create Constructor in this so c r then let's inject imapper then we say this is MPP  control period we can create an assigned field so let's use it and this what we're going to do  we're going to extend the body okay so first of all let's have our book model now this book model  is equal to the mappa class the mapper that we injected dot then map and now this map take note  we have the T destination so the destination here is what which one it is book and the so
urce here  it is a request Det go so I just going to map this request to this book it means the four properties  that we creating the request is going to assign each value to the book remaining the book ID how  do you get the book ID so we can have access to we can call this method okay to get all books  or we can even call this method so we can say that this request D2 since this is not having  you know this request2 is not having an ID but this book has an ID so when we are done it's going  to
be kept in here and then we can say that book dot we can have the ID is equal to books here.  count you see then Plus+ so the current count of the book we're going to add one to it this  is done automatically so let's see why having this so This books here it is getting a method or do you have to this should it  be account method the think it shouldn't be yeah so maybe we can just make it plus one  instead so you can see we are assigned the book automatically we get a count from the book list 
then we add plus one then we have this book model okay so after having this book model then what  we're going to do here is we are going to just add so we say that books. add then we can now  pass in this book peacefully it is very simple isn't it so now you know how to use um uh request  dto and how to also map it in here okay let's go to how do you get the book so that's to add let's  try this out and see first so we're going to run this oh we have an issue here so we need to  change this this
is not this is request dto so this is a request dto and um it must book so  I want to change this to also request dto that we have to pass in this request in here so let's  save this and now let's run this and check it out all right so let's check it  out from this book list so from an end point in here we are going to add  then if I click on a we have only title a year and I'm price let's click on try  this out we're going to add new one so we're going to say this is new book and  from the we'
re going to say this is net code then let's maintain the year as 2001 so let's try this out hit on the execute and  now let's see if this is going to be added or not so we have added right now that's going to be  let's see that's going to be ID what let's have a look so let's click on get all out and check  it out do we have it yes we have ID 11 and if you see the ID is automatically Ally generated so  you can see now we've implemented the dto and the auto mapper by next question is if I pass in
here  check it out if I pass in get ID pass in 11 check it out so from this 11 I still have the ID if you  want to skip this ID what can you do this is very simple you have to go to leten now we have when  here we have our request dto so we can have our response dto isn't it so let's create our response  dto in here and in that response dto this is what you want to do you want to combine the title  and maybe the okay you want to remove the ID then you want to display the title and also or  mayb
e the the title and on the year okay so you want to combine these two okay so I'm going to  copy this we don't need the ID let's grab this then we have to go to our dto and this going to  be a response dto so let's create a class for this before that let's stop this and our response is  uh respond from the service to a user so that's a response dto let's create a class and this will  be response respon dto now this response dto this what you want to do we want to have the title  okay the title T
he at the year and the price so from this we are not going to you see this one  we are not going to display the year you want to display the year against the title okay so we're  going to remove this from here you can clear all this okay can make this as from this end so the title and yeah that's what  we're going to do so we can add title and title and year so you going  to display the title and so the year here so after doing this what is the  first thing to do or the next thing to do is you g
o to your profile then you're  going to now create so let's copy this so here you know that we're going to  create so it's going to be from your book your book to what that is your response response D so here we have from and  on to so from books we are mapping to what that's a response dto that is the source  and that's the destination okay good so once you have this created the let's to do  here is let's go to our service and where we are getting this book list we are going  to remove all this
or let's maintain it and I'll perform it in the get by ID so with  this we we are going to put it in a code block so we can just grab this let's say this  is the book that we are looking up for so we've searched for this the book is here now when we  are done we need to um create this book isn't it so let's convert it let's map it so this  ISS map book is equal to then map do map now this map it is destination here it is what  destination is what response dto and there is a source so we are map
ping from this book to  this map book now when you check this response dto we have title in the year out and price  we're going to have properties against the out and price title and year are not going to  be assigned why let's first have a look then I explain the reason why that is happening  so when we are done we can then return map book but you see you have an ER here we cannot  convert that so what you need to do here is this must be a response dto because the state that is  here is a respo
n d so it's my sync with the name now let's go to the end point and let will do  same so from get by ID instead of having get by ID where is it get by ID yes instead of having  this is supposed to be even a list or is it a list look at this so we have this get by ID and  now this is not even a list this is just a book you see this I believe you've done it right you  created this and that's great so this is just a list but here we need to change to request  or response dto because it is returning
a response dto now let's save this let's run  this and I'll check it out you're going to have two properties been assigned but the  title and the year is going to be left out let's go in there and now get a book by  ID so we didn't one to 10 or 1 to 10 we can pass in any let's passing Eight and check it out  try this execute this and you can see title and year it is null author we have the author we  have the price isn't it let's pass in ID one then the same thing why do you have any idea yes 
the question is is it the question the answer the answer is title and year it is not found so when  it is map out when the profile is been profiling and when it is checking up mapping through the  model or the model properties in the book do you have any title and a year together no we don't so  it's going to map it directly to what it has in here so this is an exceptional one so we have  to configure an exceptions okay so how do you configure an exceptions it's very simple let's go  to the book
service in here now the book service check it out here we go to the profile and now  where we are creating the this we're going to Mar from The Source from the model to the GTO right  so with this we need to specify the exceptions now this exception is you want to add the title  and now the the year together okay so this is what you want to do from this angle we going to  you can configure this by using a simple line of code to handle this what are we doing here when  you check this we are sayi
ng that for member so what is me talking about that is a the destination  okay this is a source and that's the destination for for Destination that's why we call this DST  stands for Destination we want to get the property known as title and on year get that property from  it and map from so we are mapping we are doing an explicit mapping map form now we have this Source  The Source has this property is title and a year so combine them and I'll assign to destination  destinations title and year
property so when you have two or more you can just configure this  with that format because it is very simple right so the first thing I need to do here is to specify  the property that you want to assign or you want to work on within your destination aside from  that specify an option and that's going to be the The Source now the source here assign the  current or the required property that you want to assign to the destinations property then have  to work on that very simple one okay so now th
is is ready let's go to the book service and now  from this book service we go to get books or even get by ID okay so this get by ID it's going  to pick this automatically since we are using a request the in here and we've created a profile  for this so let's save this and now let's see if we can rerun this and I'll check it out okay  so get by ID try this out let's pass in one and I we still have the title and the year as what  now okay so let's maybe we need to um do small settings in here to
get it completed all right so  the small settings that we need to do here is to restart the application since we have um created  a profile we've worked on the profile and this profile need to be configured in the program.cs  that the stup file so we need to uh reload this application so click on this to reload the project  and now let's say it's going to work automatically for since we have a responded to being used in  here all right so let's check it out it's open now let's passing try this o
ut passing the ID  of five and now let's execute this and you must have it so you can see we have shots and have web  and it's 1952 so you can see we have title author and then price let's pass another one I'm going  to pass in 10 execute this and we have the same thing 1990 so this working isn't it yeah so one  last thing that I'm going to show you guys here it is also the reverse map what are this map  do let's see you know when you check our dto we created two um dto request and our response 
let's assume request is in the different name Al together maybe it's a general dto and with this we  have the title The the year and the price if you want to get all details excluding the ID it means  you want to have the same properties as this then we can just make it as reverse type so let's  see from here you go to your profile your mppa profile where is it and now here you know here we  are converting from the source to destination so this is a source in here you're converting from  dto to
um what book book model now if you want to convert from book model to dto with the same  format then it's better that you do dot reverse so reverse map so this going to turn this upside  down you're going to put this here and now put this here so it's going to exchange the location  the position of these models okay now how do you do this let's go to the book service and now where  we have this list okay we want to return request detail so dto you want to request  all not you want to request yo
u want to return request dto so it means  you have to clear this off now in here what can you do we're going  to use an OP mapper to map it in here so what you going to do here is  you're going to have map book this is books right so this equal to we're  going to use the mapper do map now this this map you're going to provide a list  first okay so let's provide this list of we need a list of is it this books  or request we need request dto okay because that's what you're mapping  up to so map wh
at list of request this is going to be the destination  and now where is going to be the source books so this books are going to be the source  okay you can just call this book directly or you can call this uh um um get we even  have this so we can we have to call these books and in these books here it is a list  of books that we have in here and the model here is what book but here since we created  an an a reverse map it me going to map turn upside down from the source to destination  and this
time the source going to be the entity is Stu Des going to be the dto so  with this what can we do we can return map books so let's return map books and now let's  grab this we go to the controller from here where we have all books inste of this we going  to change to request Ito although it shouldn't it shouldn't have been request here it should  be a different name because request here is what sending to database or sending to the  the service but you are rather retrieving it so should have b
een request response or maybe  General one okay this is just for a purpose test for you to get the concept okay now that  is all this very simple so now let's run this application and I'll check this this one out so  you can see that you're going to have all list excluding the IDS because we using this request  that we created and the request model has no ID inclusive okay so let's try this out from the book  all books click on this try this out execute now let's see oh yeah that's fine so you c
an see  we do not have IDs we have the title the AR and IDs out yes they are out and when you check  the profile we did nothing different all that we did was to do what yes to add reverse map  and it's going to reverse this so it's going to change the position of this this is going  to be source and is going to be destination okay and that is all so if you have a method or  a model that or D that you want to maintain the state um for both our request and our response  then it's better that you g
ive it a unique name a generalized name so that you can using it both  in a request and a response not like what I've done what I've done is a bad example okay you  cannot use request dto as a response dto no it doesn't make this clear but I want to show you  something here that's why I use that okay so do not follow this step it's very bad all right  so I think this video has actually helped you a lot to understand autoa how to configure it how  to create dto and how to work with it you know au
tomap goes with dto so anytime that it comes  to your mind to create your automap to create G don't forget to add automapper you can also  create a generic one okay so maybe when I have time I'm going to have a video done on how to  create an autoa generic one if you don't want to use this package and you want to customize  your own then you can also do that I'll look away and I'll try to have a simp video for  that all right so that's the end for this video thank you so much for watching and I'
m going  to catch up in the next video till then take care

Comments

@aldkdenedik2451

nice tutaial..thanks..greating from turkey