Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation of DTO properties which not mapped to ValueObjects #6

Open
denis-tsv opened this issue Apr 8, 2020 · 1 comment
Open

Comments

@denis-tsv
Copy link

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants