Sep 28, 2014 at 4:09 PM
Edited Oct 21, 2014 at 5:45 PM
Simple Injector contains an extension method in the MVC integration package that allows initialization/injection on MVC filter attributes. DataAnnotations however has nothing to do with MVC filter attributes and that mechanism will therefore not touch your
To allow complex business logic in validation attributes, Microsoft designed the Validation attributes to use the IServiceProvider abstraction. This allows querying the
for services that are registered in the container.
Doing this however is a terrible idea, because this leads to the
Service Locator anti-pattern
. Or more specific, attributes with logic is a terrible idea, as I explained
and Mark Seemann explained
Instead, such specific business validation deserves its own abstraction. In the applications I build, I define an
abstraction. Here the
is the type of object to validate. I use this in combination with my
commands and command handlers
where I specify one or multiple IValidator<T> implementations for a certain command. These validations are triggered inside a
command handler decorator
just before the real business logic is executed. But the exact design that suits you best depends on a lot of factors of course, but best is to keep logic out of attributes.