Database injection into a validation attribute with ASP MVC and Castle Windsor

AFAIK no dependency injection container can directly manage an attribute, since it’s instantiated by the runtime and there’s no way to intercept that.

However, they can cheat by either:

  1. Using a static gateway to the container (example), or
  2. Using a “BuildUp” feature that injects whatever dependencies are found within an already-constructed object. This is called BuildUp in Unity or InjectProperties in Autofac.

Windsor doesn’t support #2 (ref1, ref2), so you can either:

  1. Try one of the hacks to make Windsor support #2 (hack1, hack2)
  2. Use a static gateway
  3. Implement your own IValidatorBuilder and make it use Windsor to create validators. I’m sure this is implemented somewhere but I can’t find it right now…

Leave a Comment