Part 12 Why & How to use DTOs in Web/REST API || Asp.Net Core Web API Tutorials C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to web API tutorials I am venkat this is part 12 why and how to use D table so till now whatever the end points we have built we have used the database entity to return the data if you look at the repository we have built we have built a temporary repository when we are discussing about the Entity framework we will work with the real Repository but for now we are working with in-memory temporary repository so if you notice that this is an entity representing the data here okay this is the same class which will be representing the database table but if you look at our controllers we are returning the same classes we are writtening the data with same classes but I am saying that's not a good practice it's better to use the dtos instead of the database entities to explain that I will take a simple example I will take a shopping cart example so suppose this is a database and there is a data logic layer and there is a business logic layer and there is a presentation layer or UI layer there is a table so which is storing all the orders I have the order with three products one two three product a product B product C quantity is to 1 3 10 Rupees each unit and the 5 rupees each unit and 20 rupees each unit so this is the data inside my database table when I want to present this data on the UI I need to calculate total price of the order at the same time I need to add the taxes Government taxes if there are any so we cannot store this information in the database table as the taxes information will be changing so first I will read this data from database to the data logic layer I'll read that data from database to data logic layer this data logic layer will have the class just now I have showed you so here it will be the class which is holding the ID and the product name and the quantity and the unit price this is the class is required to store this information inside my C sharp so I am reading all this data and I am storing inside this class object but when I came to the business logic layer I need to calculate the total tax and the total price along with this particular class Fields I need to add more Fields like a total total and tax and tax percentage so like this I need to add additional fields to my particular class that's where when we are applying business Logic on top of the data we read from database we may need additional details that's why we need to add another class to store the data after business logic applied and this one is the data which is directly read from database we call this as a dto and we will share this dto data to the presentation layer presentation layer will present that display that now the presentation layer will display the total price and the taxes applied and the percentage of tax applied and the product name and the unit price and the quantity everything it will display let's apply the same thing here for that so we need to add the dtos first I am going to add the student dto here student dto for now in our example there is no special business logic because we are building simple demo we are using the same thing here returning we need to create object for student student dto for each students I'll create object for student dto equal to new student dto and here I'll apply ID equal to ID then name equal to m dot name address equal to item dot address email equal to dot after that I'll students dot add student sorry this is object object so my students dto object is ready we need to return like this the same thing here so what I did just now there is a repository of students I have looked through every student and I have created a student dto object for every student I have added it into the student detour list so this is how we can prepare the student dto out of the student data objects we can also build this using the link you query so that is so I'll copy this I'll show you how to build it using a link you query now let me comment this out for each Loop so this is using a link to college repository dot students Dot select n tends to yes for student so using link you we can simply read like this so what we are doing we are reading all these students from the students Repository and we are selecting while we are selecting we are converting this particular student data enter the new dto class dto object so in this we will have the list of student tt1 so like this using link you we can convert the students list into the students dto list so using link view is a better choice compared to the forage we can reduce the lines of code this is for get students and for get student by IDE we got the student here we need to return the student.to when we are returning where student dto equal to new student.to so because this one is a single record I am preparing like this student dot ID so I am preparing student dto from the student record student Dot student name email equal to student dot address equal to student dot address right that's how we can prepare the student video object and we will return the student datio so this is how we need to prepare dtos and return so when we are preparing the dtos we actually needs to apply the business logic whatever we discussed but this is a simple demo so we do not have any business logic that's why I have used the decades as it is and when we are getting student by name also will copy this and we will do this and we will return the student detail and we will change the return type as a detail and this one is the delete endpoint here nothing needs to be changed it's a simple Boolean data type return this is how we can use the dtos
Info
Channel: Nehanth World
Views: 10,871
Rating: undefined out of 5
Keywords: Part 12 Why & How to use DTOs in web API || Asp.Net Core Web API Tutorials C#, web api, c#
Id: ckMAEoBI2D4
Channel Id: undefined
Length: 8min 27sec (507 seconds)
Published: Fri Nov 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.