Shaping the Data
In this lesson, we're going to shape our returning data with the help of DTO.
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 01:27] We got an error in the last lecture and now you know the reason why. Our courses controller returns a category and when we will get the category it will have list of courses. The courses will have those categories again and this becomes a circular dependency which never ends. In this situation rather than returning our original entity which is causing this circular dependency we will return data transfer objects dto in short. DTOs are also termed as transfer objects. D TO is only used to pass data and it does not contain any business logic. Inside our API project let's create a new folder and I will call it dto. We need to create a new class which will return data instead of our course entity. Let's call it course dto. Here we can add everything we need to send to the front end. Let's copy everything from our course entity. Here we can copy everything from here because it's a lot of work to type it down and let's copy and paste it here. We need to send the id which is not mentioned here. So what I can do is and our id is of type grid and I will call it id.
[01:28 - 03:22] We need to import grid using system so I will do just that and here everything looks good and we don't need to send category id so I can take that off and instead of category I will use a string because we just want the category name and instead of requirement and learning we can make their dto as well so requirement dto and learning dto because we just need the name of the requirement and name of the learning. Nothing else. Inside the dto folder let me create a new file and a new class instead I will call it requirement dto. We want just the name which will be a string and it's a name and the same way I can create another class and I will call it learning dto. This one will have name as well so string and name here. We can use one for both of them but we might need to add something else in the future so let's keep it that way and now I will replace requirement with requirement dto and learning with learning dto. Let me first import collection from system collections generic and this is it. Now this is what we return to the user. Now that we have our dto's ready we need something that can map our dto's with the actual class for which we are going to use AutoMapper. Let's add that to our project in the next lecture.