title: How to inject dependencies used for validation in .NET MVC3?
body: <p>We have quite a few validation methods that need to access repositories / database to do their work. So far we have been using the service locator pattern (albeit sparingly) to accomplish this in custom ValidationAttributes:</p>
  
  <pre><code>public override bool IsValid(object value)
  {
      // use custom service locator in our app's infrastructure
      var repos = DependencyInjector.Current.GetService&lt;IXyzRepository&gt;();
      ...
  }
  </code></pre>
  
  <p>I know this is :( upon as an anti-pattern, and we would like to use a more correct approach. We use unity, and I read <a href="https://stackoverflow.com/q/986019/304832">this post that says to use a build-up method</a>. However <a href="http://msdn.microsoft.com/en-us/library/cc440949.aspx" rel="nofollow noreferrer">the link in the accepted answer</a> says that the documentation is outdated (retired content). </p>
  
  <p>The solution does not need to use a validation attribute, I suppose it could use IValidatableObject, however the problem remains: how to inject the dependency into the model. Do we need a custom model binder to do this? </p>
  
  <p>Another solution would be to perform the validation in the controller, where dependency injection is easy. To me this feels cluttered though. I would like the model to be validated by the time it gets to the action method.</p>
  
  <p>Also we sometimes use [RemoteAttribute] to perform some of these validations on the client. Currently those methods construct a viewmodel and delegate validation to the model by using the static Validator.TryValidateObject method. </p>
  
  <p>How have you accomplished validation that requires an injected dependency to do its work, without using the SL anti-pattern?</p>