publicclassUpdateUserRequest{ [AuthorizedUserPublisherRequired]publicintUserId{get;set;} [Required("Name is required")]publicstringName{get;set;} [ValidZipCodeRequired("Invalid zip code")]publicstringZipCode{get;set;}}publicinterfaceIUserService{voidUpdate(UpdateUserRequestrequest);}
There are validation frameworks out there that will do this, so what’s my beef? Well, first of all I want to inject the validators with dependencies to implement the juicier rules. And second, I’m treating validation as a core concern so I don’t want a dependency on a framework like Enterprise Library. I had several questions like:
How do I get dependencies into the validators?
How do I get ValidZipCodeRequired to fire with the value of the ZipCode property
This allows me to make a UnityValidatorFactory that can supply all my dependencies. Now how about this business of running the ValidZipCodeRequiredValidator with the value of the ZipCode property? For that I made a composite validator that loops through each property and runs the annotated validator against it’s property value.
Phew, we’re almost done. The only thing left is to apply this validator in configuration so I can keep the Unity reference out of my core domain. That looks like this: