Sometimes, I have really complex models with many string properties that need to be validated on setting, however the validation doesn't often go further than IsNotNullOrWhitespace.
This often leads to unnecessary code repetition, so I wondered if there's a way to automatically validate property setter values, preferably without any additional framework.
Possible Solutions
- AOP (e.g. with PostSharp)
- Fluent validation
- Data Annotations
Data Annotations feels as the most natural way to me, as the validation is very close to the model and since it's part of the .Net-Framework, attributes are okay. However, if I'm using the model outside of MVC or serialization, I have to do the validation manually using a validator. So I have to do the validation probably at many places (repositories, APIs, services) and if I forget about doing this somewhere, my domain rules can be broken.
AOP might be the perfect way, however there isn't such a thing directly in C# and tightly coupling my domain models to a infrastructure component like PostSharp or Ninject (interception) is a no-no.