How To Use DTOs In .NET REST APIs

  Рет қаралды 21,905

Julio Casal

Julio Casal

Күн бұрын

💻Watch the full tutorial: • ASP.NET Core Tutorial ...
A Data Transfer Object, or DTO, represents a shared agreement between a client and a server about how data will be transferred and used.
In this video I explain why you should use DTOs and then I go over a practical implementation in a simple C# based .NET REST API.
🔥Become a .NET Cloud Developer: juliocasal.com...
🗺️Get My Free .NET Backend Developer Roadmap: juliocasal.com...
Join me on Patreon: / juliocasal
Follow me on LinkedIn: / juliocasal
Follow me on X: x.com/julioc
#csharp #dotnet #aspnetcore

Пікірлер: 45
@failscript
@failscript Жыл бұрын
By C# conventions you should use "toDTO" instead of "asDTO" since the method returns a new object. The "as" word is used for when you want to imply that something is being casted AS something.
@juliocasal
@juliocasal Жыл бұрын
Good call, agree!
@AndrésGonzález-k5w
@AndrésGonzález-k5w Ай бұрын
I learned this trick in my job! it's very usefull
@juliocasal
@juliocasal Ай бұрын
Glad it helped!
@josecarlosmacoratti
@josecarlosmacoratti Жыл бұрын
Great !!! Using extension methods is the best way in a simple scenario .
@juliocasal
@juliocasal Жыл бұрын
Yes it is!
@baranacikgoz
@baranacikgoz Жыл бұрын
I prefer project to dto feature of entity framework [ like _dbContext.MyUsers.Select(x => new MyDto(x.Name, x.Surname)) ], or you may achieve the same thing with dapper. No need for a dto conversion again in this way, unless you're dealing with in-memory collections
@juliocasal
@juliocasal Жыл бұрын
The data components should have no need to deal with DTOs, which goes for EF code too. Entity to DTO conversion is a higher level concern.
@pazzuto
@pazzuto Жыл бұрын
I do this for my "queries" (CQRS). I only bring back what I need from the database straight into a DTO whether it's EF, Dapper, or ADO. In some cases, I create a SQL View and I do a straight map. To alleviate the issue as Julio described for the endpoint changing, there's yet another object usually called a contract (following REPR): Entity Object -> DTO -> Response/Contract. But, I do use a DTO as the contract in lots of cases.
@rikkoo
@rikkoo 6 ай бұрын
beautiful
@perelium-x
@perelium-x 6 ай бұрын
Ikrrrr
@podeig
@podeig Жыл бұрын
Super tutorial, Easy explained and precisely what I needed. Thank you!
@juliocasal
@juliocasal Жыл бұрын
Great to hear!
@torrvic1156
@torrvic1156 Жыл бұрын
Thank you so much! That was exactly what I searched for but however I apologise sir but as far as I know it goes against the Clean Code principles to use decorations on your Entities. They should be as clear as possible because they correspond to the tables of your database. And is it Ok to use decorations with your DTOs also? Or do I don't understand something? And also why your records are not in different files but in one Julio? It looks not Ok in my opinion.
@juliocasal
@juliocasal Жыл бұрын
Yes, it would be best to keep the data annotations out of the entities. I don't see a problem with using them in DTOs. For a small set of DTOs, a single file is OK. If you have many, a file per DTO would be better.
@saddamhossaindotnet
@saddamhossaindotnet Жыл бұрын
Great tutorial! Is it possible to utilize extension methods for complex DTO mapping without any external libraries? Do you consistently employ extension methods for DTOs without relying on any other mapping libraries?
@juliocasal
@juliocasal Жыл бұрын
Yes, absolutely
@goqsane
@goqsane Жыл бұрын
@@juliocasalthis is what we do too, extension methods .ToDto.
@juliocasal
@juliocasal Жыл бұрын
@@goqsaneGreat!
@perelium-x
@perelium-x 6 ай бұрын
Tysm
@electronicheartbreak.
@electronicheartbreak. 6 ай бұрын
I have some questions: 1. Why do you add validation on the DTO, since input CAN and "should" be invalid? You also now have duplication in validation rules. 2. Since you added validation on the DTO, you do not check the validity of the input by if Modelstate.IsValid. Why?
@juliocasal
@juliocasal 6 ай бұрын
The DTO is the input. What would you validate?
@livan3li08
@livan3li08 11 ай бұрын
can you share what the extension you use when creating c# files by right clicking in vs code explorer menu?
@juliocasal
@juliocasal 11 ай бұрын
Here: marketplace.visualstudio.com/items?itemName=kreativ-software.csharpextensions
@livan3li08
@livan3li08 11 ай бұрын
@@juliocasal thank you very much.
@pazzuto
@pazzuto Жыл бұрын
I'm surprise that for Post/Put/Delete, you did not use extensions. You could extend DTO just like entity with a method like ToEntity. That way, your mapping is all in the extensions class, and your endpoint doesn't need to know about this conversion.
@juliocasal
@juliocasal Жыл бұрын
Great tip!
@faridulhuk1248
@faridulhuk1248 Жыл бұрын
Is it possible to create Mapper for Mapping dto to Entity , reverse way
@juliocasal
@juliocasal Жыл бұрын
Everything is possible!
@redouane5626
@redouane5626 Жыл бұрын
I have habit that I do not use DTOs for API return types, instead I call them Models, is this right or wrong?
@juliocasal
@juliocasal Жыл бұрын
The name is not as important as the fact that you should not return the class that directly represents your DB schema. Sometimes I also call this Request or Response.
@benchmarks1016
@benchmarks1016 Жыл бұрын
upto 2:20
@somaticHuman
@somaticHuman Жыл бұрын
I prefer to use user-defined explicit conversion operators inside DTOs, instead of extension methods...
@santhnu
@santhnu Жыл бұрын
Would love to know how you do that? Would it offer any advantages/ease of use over extension methods? Thanks.
@agat366
@agat366 Жыл бұрын
From my long experience, it’s quite inconvenient and time-consuming to define all the transformations. Imagine if you have several dozens of DTOs, also, it’s inconvenient as you have to make all the transformations changes manually after refactoring. I believe, more standard approach is auto-mapping. The only thing I would consider would be embedding AutoMapper into the AsDto extension methods.
@juliocasal
@juliocasal Жыл бұрын
Thanks, not a big fan of AutoMapper, and extension methods have been working great for me. But it's all a matter of personal taste!
@goqsane
@goqsane Жыл бұрын
Horrible advice. Don't follow.
@coding-esmaster3259
@coding-esmaster3259 Жыл бұрын
Uff I would recommend not to use auto-mapping for projects does are not prototypes, auto-mapping becomes a headache when you change your models and you only get the errors in run-time instead of compilation and that's it is only example of another issues that you inherits by using AutoMapper.
@sagarmajumdar92
@sagarmajumdar92 Жыл бұрын
​​@@goqsane which one is the bad advice? What is shown in this video or using automapper
@goqsane
@goqsane Жыл бұрын
@@sagarmajumdar92using automapper. It'll bite you in the long run.
@iteospace
@iteospace 3 күн бұрын
Dont use DTO
@juliocasal
@juliocasal 3 күн бұрын
?
@iteospace
@iteospace 3 күн бұрын
@@juliocasal i use clean architecture and my project not contains any object named Dto
C# Logging In 100 seconds
1:42
Julio Casal
Рет қаралды 20 М.
Why do we use DTOs in our APIs?
26:15
Codewrinkles
Рет қаралды 9 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
5 Rules For DTOs
17:56
Ardalis
Рет қаралды 48 М.
Don't Use Polly in .NET Directly. Use this instead!
14:58
Nick Chapsas
Рет қаралды 78 М.
How To Test .NET REST APIs
26:17
Julio Casal
Рет қаралды 6 М.
Brutally honest advice for new .NET Web Developers
7:19
Ed Andersen
Рет қаралды 329 М.
Why to use DTO (Data Transfer Objects)
11:02
Study Mash
Рет қаралды 33 М.
How To Store Secrets For Local Development
7:57
Julio Casal
Рет қаралды 4,8 М.
Building better DTOs in C#
11:57
Gui Ferreira
Рет қаралды 8 М.
Use Data Transfer Objects (DTOs) in .NET the Right Way 🚀
6:48
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН