Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 4
    "When is it advisable to separate DTO from JSON?" Never? That's one of the main reasons DTOs exist: to serialize them into any format you want. Would you also create a separate class for XML? CSV? All of them exactly the same? Form over content. Commented Dec 3, 2024 at 6:17
  • 7
    "We know that entities that are also DTOs and also REST API JSONs won't pass code review": So, your company has a guideline that doesn't allow that. Depending on the project complexity, and ignoring any company guidelines, anything can be a good solution, from one-class-does-it-all to a confusingly complex zoo of classes and interfaces, all representing the same concept, just for different purposes. Whoever tells you "you have to do it exactly this way" should also state the circumstances where this advice applies. Commented Dec 3, 2024 at 8:03
  • 1
    @RalfKleberhoff Thank you for your comment. Please post it as an answer and I will accept it. In the answer, if you could elaborate on an example that justifies not following the "Never" rule explained in the previous comment, that would help me better understand. Commented Dec 3, 2024 at 13:50
  • 2
    On second thought, I voted to reopen. There are some valid technical considerations that affect this decision even if there is an element of subjectivity to it. Commented Dec 3, 2024 at 14:27
  • 3
    This question seems to be built on inaccurate language. Case in point: "separate JSON classes allow us to handle version-specific serialization without affecting your internal DTOs, right?" What you're referring to as "JSON class" here is what is commonly referred to as a DTO. What you're referring to as an "internal DTO" is generally referred to as a domain model (or similarly private data structure). Commented Dec 3, 2024 at 23:14