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!

Brad Urani
  • 1,429
  • 1
  • 16
  • 29
  • Related: http://stackoverflow.com/questions/6126663/injecting-dependency-into-customattribute-using-castle-windsor – Steven Jun 09 '11 at 07:50

1 Answers1

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.

eulerfx
  • 36,769
  • 7
  • 61
  • 83
  • 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. – Brad Urani Jun 10 '11 at 15:02