1

So ASP.Net MVC 3 has extensibility points to allow dependency injection in everything, it seems, except data annotations (validation attributes). Is there a place in the MVC framework where I can call setters on all data annotations before they're used to validate the models on a form post? Thanks!

1

1 Answer 1

3

Dependency injection cannot be directly used on data annotation attributes because the properties of an attribute are determined at compile time. To achieve your desired result you can extend the attributes so that when they are invoked they retrieve custom behavior from a service locator such as DependencyResolver.

However, it would be helpful to have more information about what you are trying to achieve. Perhaps calling setters on validation attributes is not the best approach.

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting, I'll have to read more about attributes. I was hoping for a way to pass an instance of a Unity container into the data annotations at some point in the MVC page request lifecycle before validation is run. I ended up using a static singleton to get it, but I was trying to avoid that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.