Use Data Transfer Objects (DTOs) in .NET the Right Way 🚀

preview_player
Показать описание
---
Рекомендации по теме
Комментарии
Автор

DTO's also great for when you need to add annotations to properties since those probably shouldn't be added directly to the data model (especially in EF if you're doing db-first and need to regenerate them due to changes).

dasfahrer
Автор

It’s a good practice, I believe. After being using AutoMapper for years I ended up doing a small class to write my mappings without an external library. Either way, with libraries or any other approaches, the main goal is seizing the benefits from the DTO. More code but cleaner code. We normally have to write that code once and that is barely touched again.

r.osorio
Автор

Simple and straight forward. Have never used Mapster though for mapping, will try it out.

nlisimalaba
Автор

Since Blazor, I'm done with DTO's. I put my Entity models right in the client project. 99% of Entities are pretty much known, like an order and order items. Just makes the code easier, faster to build.

If there's something I don't want to expose to the user, I'll slap a [JsonIgnore] attribute on it. Yes, the end user could decompile the WASM and see the fields, but there'd be no "hidden" data.

On the rare occasion I really do need to keep sensitive data from even hitting the browser in any way/shape/fashion or form, then yes, I'll put that entity in the server and use a DTO.

and if you're gonna use a mapper, like others in the comments are saying, Mapperly all day, ALL DAY! Quick, easy, and runs at build time, so no overhead startup.

mtranchi
Автор

I haven’t used Mapster. I usually add a constructor to the DTO that takes a model.

yumyum
Автор

Really would like a video about Mapperly. It's a source generator, even faster then Maspter and easy as well

joaogabrielv.m
Автор

I think the best use case of DTO at least in my projects is projection when you can select exactly what you need from data base instead of select *

afshin
Автор

I am with Nick Chapsas on auto mappers: don't use them. Your code will happily compile and run even when the auto mapping is broken.

Rick-mfgh
Автор

At the monent my DTOs are still WCF Contracts. :-) In a pilot project I went the "gRPC First" route. I still have hopes for gRPC to WCF trancoding.

_miranHorvat
Автор

Mapperly is a very good mapper ... uses source generators

JayTandon-nqdu
Автор

I use Auto Mapper extension what difference between Auto Mapper and Mapster? Thanks for your answer

AliyProgrammer
Автор

Great video! Is there any chance you could make a tutorial on how to best set up projection code for DTOs? I haven't found a good way to write reusable projections because it either needs to happen in memory after pulling full objects from the database, or I have to write the mappings by hand every single time I do a select to get good performance. But that is really cumbersome especially with DTOs that have multiple levels of data (which are also DTOs)

HaeriStudios
Автор

Entity -> DTO -(cloud)-> (what here?) -> Model in MVC/MVVM
Where on client side DTO should be mapped to other object, immediately?

adam-xtte
Автор

Nice, but this will stil load all the data from the database?

nieuwveen
Автор

Really Great Explaination but are you looking for a video editor?

mohammedsaqlainshaikh
Автор

1:28 You said that DTO transfers data between layers of your application. What are the layers between which DTO in your example transfers data?

EgorUshakov
Автор

May i suggest to have DTO as sealed record, instead of class.

rick_info_dev_pyt
Автор

Do I need a DTO if my Entities have to recurve extra fields in order for EntityFramework to pick up and make the right relations?
Like I have an Entity that needs only 3 fields but when I read it from a GET request I get like other 3 fields set to null, when I try to .Include() it says it’s doing an infinite cycle. :(

Luisllaboj
Автор

wait i didn’t know with mapster you don’t have to explicitly define the maps you make in a mapper class like in automapper… actually might check out mapster for this reason lol

camrws
Автор

How may i get connected with you for video editing process?

STech_Videos