vkhorikov / AnemicDomainModel

Source code for the Anemic Domain Model Pluralsight course
MIT License
282 stars 92 forks source link

Validation of DTO properties which not mapped to ValueObjects #6

Open denis-tsv opened 4 years ago

denis-tsv commented 4 years ago

Hello Vladimir. Thank you for course "Refactoring from anemic to rich model"! I have a question about validation. We removed all validation attributes (for example Required) from DTO. Also we removed checking of model state from controller (if (!ModelState.IsValid) return BadRequest();) And we moved validation to ValueObjects. Ok. But how we will validate properties from DTO which not mapped to ValueObjects? Lets imagine that Customer entity has one more string property Name1 with length < 50 (and this property is not value object). Where we will check this constraint? We will add at attribute StringLenght(50) to property in DTO and return checking of Model state in controller? Or we will use another solution, for example validation of Name1.Length in constructor of Customer entity?

vkhorikov commented 4 years ago

I recommend this approach for error handling: https://enterprisecraftsmanship.com/posts/combining-asp-net-core-attributes-with-value-objects/

This article assumes you have a value object and a corresponding attribute for all primitive types (such as CustomerName), but you don't have to -- you can create a generic StringLenght attribute and use it for properties you don't have a VO for.