Building better DTOs in C#

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024
  • How to build better DTOS in C# dotnet
    💎 Be a Patreon to get the source code: / gsferreira
    🚨 KEY LINKS
    🤝 Support me on Patreon (and get access to source code) here: / gsferreira
    💌 Free Developer Insights: guiferreira.me...
    📘 Courses: guiferreira.me...
    👨‍🏫 Workshops: guiferreira.me...
    🔗 GET IN TOUCH
    LinkedIn: / gferreira
    Twitter: / gsferreira
    GitHub: github.com/gsf...
    Get my newsletters: guiferreira.me...
    Visit my blog: guiferreira.me
    👋 WHO AM I
    Hey! If you're new to my Channel, I'm Guilherme. Call me Gui. I'm a Minimalist Software Craftsman. What do I do here? I share tips on how to simplify your life as a Developer.
    #csharp #dotnet

ความคิดเห็น • 19

  • @christianschulz4475
    @christianschulz4475 10 วันที่ผ่านมา +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.

    • @gui.ferreira
      @gui.ferreira  8 วันที่ผ่านมา

      Thanks for sharing!

  • @allannielsen4752
    @allannielsen4752 11 วันที่ผ่านมา +6

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

  • @bl1tz229
    @bl1tz229 10 วันที่ผ่านมา

    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 10 วันที่ผ่านมา

      Hmm. What do you mean, any example?

  • @Blazor77756
    @Blazor77756 11 วันที่ผ่านมา

    thanks

  • @richardhaughton9633
    @richardhaughton9633 11 วันที่ผ่านมา

    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  8 วันที่ผ่านมา

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

  • @webbi1540
    @webbi1540 10 วันที่ผ่านมา

    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.

  • @AboutCleanCode
    @AboutCleanCode 11 วันที่ผ่านมา +1

    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 ▶ th-cam.com/video/GJ8f9r97YTA/w-d-xo.html

    • @WhichWazzockWantedEmailHandles
      @WhichWazzockWantedEmailHandles 10 วันที่ผ่านมา

      I love how you explained the 'required' keyword, your channel needs more subscribers.

  • @alfonsdeda8912
    @alfonsdeda8912 10 วันที่ผ่านมา

    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 10 วันที่ผ่านมา

      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  8 วันที่ผ่านมา

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

    • @alfonsdeda8912
      @alfonsdeda8912 8 วันที่ผ่านมา

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

    • @gui.ferreira
      @gui.ferreira  8 วันที่ผ่านมา

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

    • @alfonsdeda8912
      @alfonsdeda8912 8 วันที่ผ่านมา

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

  • @BloomfieldBlanche
    @BloomfieldBlanche 6 วันที่ผ่านมา

    Moore Kenneth Hall Daniel White Melissa