I am building an ASP.NET MVC project and going for the following Architecture:
- A Core project that has models, validation, dto, logic, etc.
- A ServiceStack API project that acts as my REST API
- An ASP.NET MVC Web project that is the UI
So, let's say I want to add a user. I define a NewUserInputModel
in the Core project. I give it some data annotations such as [Required]
. After doing this, the Web project will perform client side validation based on those annotations.
My question is about server side validation. I want to validate the NewUserInputModel
using the same rules that are being used on the client side, and I want to run that validation weather the NewUserInputModel
comes in from the API or the Web project.
I realize I could call ModelState.IsValid
from a Controller in the Web project - but I want to call that validation from the Core project so that all validation logic lives in Core. This way, no matter how this model gets to the Core logic, I always call the same validation. I don't want to leak a System.Web
reference into my Core project.
Is this a reasonable design? I think it is - but if something about it smells, I'd be happy to hear it.
Thanks in advance for any help.
TryValidateObject()
also has a version with a 4th parametervaldateAllProperties
, that, when set to true, causes the validator to process attributes besides theRequiredAttribute
. – Rondelle