user11081980
user11081980

Reputation: 3289

Should I add validation to DTOs in a DDD architecture?

I typically use DTOs to send requests from Presentation to Application and I use DTOs to send back responses from Application back to Presentation.

I typically decorate the properties of my request DTOs with attributes such as Required, StringLength and so for. But no business rules.

However, I have read in multiple sources that I shouldn't add any sort of validation (such as attributes such as Required, StringLength, etc.) to DTOs at all.

I would like to ask what is the recommended way in light of the DDD literature. Thank you.

Upvotes: 3

Views: 682

Answers (1)

theDmi
theDmi

Reputation: 18034

The concept within DDD that is responsible for input validation is the application service.

So you should remove the attributes and validate the DTOs in the app service. Use the DTOs to define structure only, and perform input validation in the app service.

If you get a lot of duplicated code in different app services (e.g. because they use the same DTO and require the same validation), you can of course extract the validation into a reusable service. Still, the app service controls input validation.

Upvotes: 6

Related Questions