Why do we use DTOs in our APIs?

Поділитися
Вставка
  • Опубліковано 21 тра 2024
  • Why do we use DTOs in our APIs? That's a question I have received often as comment to different videos om ASP.Net Core WebAPis. In this video we try to demystify the concept of DTOs and why do we use them? A short answer is that we don't want to expose full domain models in our API contracts, as those models usually contain a lot of information that is not needed to by the consumers of our different endpoints. But there's also a technical reason why DTOs are helpful, as we might get a lot of Json serialization exceptions if we don't use them, especially when we have many-to-many relationships.
    Chapters:
    1. Intro 00:00
    2. Explaining the setup 00:55
    3. First reason: exposing data that is not needed in the current endpoint context 03:20
    4. Second reason: requiring data that is not needed in request bodies 05:33
    5. Third reason: JSON serialization exceptions 08:31
    6. Implementing DTOs (without AutoMapper) 12:14
    7. Summary 23:27
    AutoMapper tutorial: • How to implement AutoM...
    #AspNetCore #dotnet #csharp
  • Наука та технологія

КОМЕНТАРІ • 21

  • @fadidib8516
    @fadidib8516 Рік тому +1

    Great Channel, I love the deep dive of information.

  • @shaileshkuswaha1126
    @shaileshkuswaha1126 Рік тому

    Such a Amazing tutorial. Thank you

  • @juliancavallo
    @juliancavallo 2 роки тому +1

    Thank you for this video man, I always had doubts about DTOs and now it's all clear. Keep up the good work!

    • @Codewrinkles
      @Codewrinkles  2 роки тому +1

      I'm glad it helped you clear up the reasons why we use DTOs.

  • @arunbm123
    @arunbm123 Рік тому

    really amazing informative Video..Thank u

  • @donmikkodanm.olmillo8154
    @donmikkodanm.olmillo8154 2 роки тому +1

    Thank you for this video. It really helped me cleared about why we need to map it into DTOs. To put it on simple words, DTOs are much more flexible, and it is towards more on the client-side where the end user will modify or get the data. I've also heard from my mentors that you can also combine two domain models into one DTO but that is more on advanced one.

    • @Codewrinkles
      @Codewrinkles  2 роки тому +2

      Tes, on the DTO you can combine what ever you want. A DTO is basically an aggregate of different data that will be consumed by the clients of the API. Id doesn't have to match 1 to 1 with your domain model classes.

  • @abdullohprogram
    @abdullohprogram 2 роки тому

    thanks a lot :) was vey useful dont stop :)

  • @abdulwahabkhan4316
    @abdulwahabkhan4316 2 роки тому

    very well explained :)

  • @vu5700
    @vu5700 Рік тому

    Great video sir :)

  • @ahmedalhallag3338
    @ahmedalhallag3338 Рік тому

    The cleanest video on DTOs! A sub from me

    • @Codewrinkles
      @Codewrinkles  Рік тому

      I'm glad. I'm not sure why but it seems there is a lot of confusion and misunderstanding regarding DTOs. I'm happy this video cleared up some aspects for you.

  • @godfather7695
    @godfather7695 3 місяці тому

    11:50 you may initialize ICollection by emty List or array and there is no possible nullref ex anymore

  • @alfonsdeda8912
    @alfonsdeda8912 6 місяців тому

    Hi, nice video!
    Two questions:
    -I created the service class that access the database with ef core, service's methods work with entities db, this service is called from the controller in api, so should i do mapping in service or controller?
    -if i want different dtos of same object from the same controller should i do two different endpoints?
    Thanks in advance.

  • @steve-o2198
    @steve-o2198 7 місяців тому +1

    Using linq/list projection would be better and cleaner solution to mapping entities to dtos instead of doing loops.

  • @royvethanayagam5888
    @royvethanayagam5888 2 роки тому +1

    Superb.

  • @krisztianistvancsato5940
    @krisztianistvancsato5940 2 роки тому +1

    Nicely structured. Very clear. Great stuff. Thanks a lot!
    Just one question: Is there a way to connect my domain models and my dtos?
    So let's say, that I need to change some properties, for example in one of my domains.
    I'd like, if it automatically changed it in the related dto or it offered me the option. Thanks in advance.

    • @Codewrinkles
      @Codewrinkles  2 роки тому

      AutoMapper mapping is a one-off action. If something changes automatically in your domain model after the mapping, the change will not automatically be replicated into your DTO. If you want to achieve something like that you might need to work with evens and re-do the mapping when certain events are raised. I am not aware of anything else to achieve your goal.

    • @krisztianistvancsato5940
      @krisztianistvancsato5940 2 роки тому +1

      @@Codewrinkles I guess that can be an idea for a personal project, right? ;-)
      Once again, thank you very much for the excellent video.

  • @TheNefelin
    @TheNefelin Місяць тому

    is this project on GitHub?