Building better DTOs in C#

Поделиться
HTML-код
  • Опубликовано: 20 янв 2025

Комментарии • 23

  • @allannielsen4752
    @allannielsen4752 4 месяца назад +8

    I use records too, but only include mandatory fields in constructor, optional are init properties in the record.

  • @christianschulz4475
    @christianschulz4475 4 месяца назад +3

    I can support all your recommendations. Had an app with generated DTO and few additional methods. And inheritance from a basic DTO. Over the years, this has evolved into DTOs for every use case. Especially the division into request and response DTO has made the code more maintainable.

  • @kitsurubami
    @kitsurubami 3 месяца назад +1

    Love this video. Thank you!

  • @jindhk
    @jindhk 4 месяца назад

    How do we make authentication between those dll calls that will not greatly slow the dll's we are using for dto. Any package suggestion?

    • @marcobaccaro
      @marcobaccaro 3 месяца назад

      What kind of question is that? It makes no sense at all.

  • @bl1tz229
    @bl1tz229 4 месяца назад

    Watch out when you have to convert a DTO to a database record using the “id” field. Since the “id” field will try to automatically map towards the id field of the database record, resulting in an InvalidCastException (since often, a database id is defined as integer)

    • @thelostrider1
      @thelostrider1 4 месяца назад

      Hmm. What do you mean, any example?

  • @alfonsdeda8912
    @alfonsdeda8912 4 месяца назад

    Hi, if I don't have all dto parameters in one time, for example in blazor form, should i use two different models, one mutable that stores form datas and One immutable dto that is built from the mutable model?

    • @DieDona
      @DieDona 4 месяца назад

      Most likely you are not in need of a dto. Assuming you are on the frontend with blazor, you will need a mutable object to fill in the data.
      Then, when all your data is ready to be sent over to the backend, create a dto.
      In short, if you are handling an object, causing mutation, most like it's not a dto.

    • @gui.ferreira
      @gui.ferreira  4 месяца назад

      I have no experience with blazor. Isn't it the case of MVVM? Model-view-view-model?

    • @alfonsdeda8912
      @alfonsdeda8912 4 месяца назад

      @@gui.ferreira not exactly, Is component based model

    • @gui.ferreira
      @gui.ferreira  4 месяца назад

      @@alfonsdeda8912 On that case, I don't see the object that supports the component as a DTO.

    • @alfonsdeda8912
      @alfonsdeda8912 4 месяца назад

      So i have to create One model to store mutable component data and create before saving the dto, Is correct?

  • @richardhaughton9633
    @richardhaughton9633 4 месяца назад

    What about readonly computed properties like FullName => $"{FirstName} {LastName}". I often use readonly computed properties which transforms the data iny my Dtos. The other system will just see it as another property. What you think about it?

    • @gui.ferreira
      @gui.ferreira  4 месяца назад

      There are some use cases where that approach is useful. Example: retro compatible changes

  • @codeDotnet
    @codeDotnet 4 месяца назад

    thanks

  • @AboutCleanCode
    @AboutCleanCode 4 месяца назад +2

    records are a very convenient and concise way to define data structures but there is no simple way to validate that the parameters passed to the primary constructor are valid. I found some trick recently, but it is not very intuitive so i rarely use it ▶ ruclips.net/video/GJ8f9r97YTA/видео.html

  • @webbi1540
    @webbi1540 4 месяца назад

    Its not a matter of preferrence. Should always be using record for safety today. Thats the problem with c#, so many alternatives which doesn’t good anymore.

  • @BloomfieldBlanche
    @BloomfieldBlanche 4 месяца назад

    Moore Kenneth Hall Daniel White Melissa