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
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.
Thanks for sharing!
I use records too, but only include mandatory fields in constructor, optional are init properties in the record.
Great approach
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)
Hmm. What do you mean, any example?
thanks
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?
There are some use cases where that approach is useful. Example: retro compatible changes
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.
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
I love how you explained the 'required' keyword, your channel needs more subscribers.
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?
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.
I have no experience with blazor. Isn't it the case of MVVM? Model-view-view-model?
@@gui.ferreira not exactly, Is component based model
@@alfonsdeda8912 On that case, I don't see the object that supports the component as a DTO.
So i have to create One model to store mutable component data and create before saving the dto, Is correct?
Moore Kenneth Hall Daniel White Melissa