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*

15
  • 11
    "You would be breaking the DRY principle putting that validation logic everywhere a zip code is used." That is not true. Validation should be done as soon as the data is entered into your module. If there is more than one "entry point" the validation should be in a reusable unit, and that does not need to be (nor should be) the DTO... Commented Jan 31, 2018 at 10:22
  • 1
    How are you giving "mindate" and "today's date" to DateOfBirth's constructor for it to check against? Commented Jan 31, 2018 at 10:24
  • 18
    Another benefit of creating custom types is type safety. If you have Salary and Distance object you can't accidentally use them interchangeably. You can if they are both of type double. Commented Jan 31, 2018 at 12:16
  • 3
    @w0051977 You statement (as I understood it) implied that anything else than having the validation in the DTOs constructor would violate DRY. In fact the validation should be outside the DTO... Commented Jan 31, 2018 at 12:22
  • 2
    To me it's all a matter of scope. If you give primitives a wide scope, then there are numerous ways in which they can be misused and mishandled. So you generally want to give them a narrower scope, and one way to do that is to design a class representing a concept using a primitive, privately stored as an internal, to implement it. Now the scope of the primitive is narrow and is unlikely to be misused/mishandled, and you can effectively maintain invariants. But if the primitive's scope was narrow to begin with, this might be overkill and introduce a lot of extra coupling and code to maintain. Commented Jan 31, 2018 at 14:12