ASP.NET Core Web API .NET 8 2024 - 5. DTOs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so if you are familiar with net in any capacity I guarantee you you've heard this acronym before dto dto stands for data transfer object and the thing about dto is that anything in programming could be defined as a dto because the term is so vague but in the context of net core and in the context of making apis 90% of DT are going to fall under this response request dto format and all a dto really does is it spruces up your dto many times you don't want to return a whole entire object back to the user many times you don't want to place a whole entire huge object inside of your database you want to trim it down what exactly do I mean by this let's give an example let's say that I am trying to return the username back from the API and we want the username but the model is going to also consist of a password as well too and I don't know about you but we never want to return a password especially in clear teex so what are we going to do we have to get rid of this password if we are just going to return the username and the way that we do that is we make a dto the second is going to be the request whenever we are submitting data to our API most of the time we are not going to send data in the whole entire model format we are going to ask the user we're going to ask the front-end developer to submit data in a particular format and most of the time we don't want them to send the whole entire model so we have dto to trim this down and also so that we can place data annotations on our properties this is getting a little ahead of ourselves we are going to talk talk about data validation in a further video but right now let's just focus on the response and trimming down our dto when we return them to the user without automapper let's go ahead and hop inside BS code okay so first things first let's create our dto for our stock and I'm going to create a folder going to call this dtos and I am also going to get ahead get out in front of this and actually separate these into different folders because you're going to be creating a lot of dto probably and you just want to go ahead and start separating these by the model different ways that you could do it but I like doing it like this okay so first thing we're just going to create our default stock dto model now the stock dto is going to make it so that when we get responses back let's just say we don't want to receive our comments I think every I think everything looks good in here but for right now we don't want to receive comments because we haven't made any comments and we need to practice a little bit so what I'm going to do is just go ahead go inside of my stock model and transfer all of this over to the stock dto now we do have some data annotations here but we can go ahead and actually get rid of these and we can rely on the actual model to do the uh decimal and we you could just leave everything like it is now remember down here is where comments were comments were here previously but comments are no longer going to be transmitted back from the API because let's just say for instance we need to get rid of the comments for some reason right now we're just building out we're going to bring comments back but right now we're just practicing so we're going to do this without automapper I actually have borrowed a lot of this from java the way that I do mappers is very Java esque and you could totally do this with automapper but I'm not the biggest automapper fan and I think it's just easier just to create mappers on your own so what I'm going to do is create a stock mappers do class right here and I'm I'm going to put all of my mappers in this class also these are going to be extension methods so we're going to have to declare this static and we need to go down here and we're going to create our very first method and let me see here so static we will return our stock dto because this is the type of data that we want back and we're going to call this to stock dto and the great thing about net is because these are extension methods these look awesome I wish Java did something like this with Java you just have to create functions to do this but with net you can easily just create little extension methods and they look awesome so we're going to go ahead since we want to return a stock dto we're going to return a new stock dto down here and let's go ahead and type it all out so go ID this is going to be stock model and we're going to return we're going to actually return the ID we do want the ID and feel free to trim out anything that you don't want maybe you don't want the symbol maybe you don't want the company name you could easily trim these and it'll make it so that you won't get back the certain type of data that you don't put in here but if you do put this in here what's going to happen is that it's going to reshape the data and it kind of intuitively just makes sense you're just creating a new object and you're returning whatever you put into the actual extension method you're going to be placing it within a new object and just returning a new object so it does kind of look a little confusing at first but if you just kind of Trace what's going where a lot of times you can just figure it out we have the purchase stock model. purchase so keep going back down uh the last dividend so say last div is equal to stock model. last div and Industry is equal to stock model. industry and we'll also go ahead down here and we're going to have our market cap so we have market cap and that is pretty much it so what we need to do now is we need to go back to our stop controller so what we need to do is we need to go into here and we're going to do a select now you can't actually just tag this to stock dto on the end of it like this you have to do a select and a select if you're coming from the world of JavaScript a select is pretty much a mapper so I'm assuming you may have already completed the react portion of this a select is uh Net's version of map so it's going to return a immutable array or an immutable list of this two stock dto otherwise if we just try to tack it onto the list we're not it's a list of things and it's expecting just a solo stock dto so if you want to be able to actually uh iterate through it and transfer everything to a stock dto just remember that you're going to have to do the select and the select is the same thing as a map then we go down here and with this one this one is going to be easy peasy we just go down here and just go ahead and Tack it on and also that's pretty much it so let's go down here and let's make sure everything's running correctly and we're actually uh Transforming Our data so let's go over here I'm going to go get all the stocks and would you look at that that beautiful data no comments in that data beautiful looking data then we're going to go ahead here let's grab Let's test our Tesla stock again and I'm going to just grab 21 here I guess I don't need to really copy it I can just go ahead and just push paste in 21 here so go ahead go ahead execute that and we get our uh Tesla stock back and we do not have comments any anymore we have successfully applied dto to our project anyways hope that you guys enjoyed this if you did make sure to smash that like button smash that subscribe button and as always thank you for watching
Info
Channel: Teddy Smith
Views: 3,803
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: Mxm81T7waO8
Channel Id: undefined
Length: 8min 26sec (506 seconds)
Published: Fri Jan 05 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.