C
C#3mo ago
Core

DTO validation with DRY principle (without repeating the same validation)

Hello, I would like to know what is used in practice, regarding the DTO validation. Let's say for 2 endpoints I would have nearly the same DTO, so the same properties would have the exact same annotations. As a result, code duplication would occur, and I would end up copying the lines back and forth. Is this what happens in practice?
2 Replies
Joschi
Joschi3mo ago
If you are really sure, that specific parts have to stay in sync, you could extract those into shared classes and use composition in your dto. But that again introduces complexity and coupling. That may not be worth it. It will also blow up as soon as you realise those parts shouldn't be coupled after all. To summarise, unless it is absolutely critical, that those properties have the same validation at all times I wouldn't bother and just ignore DRY. Iirc. If you use fluent validation you could write new rules, which are reusable. That wouldn't be such a big problem, when you decide to diverge the validation later on.
Core
Core3mo ago
Thank you. I did not know about Fluent Validation. It looks like it's exactly what I need