title: Database injection into a validation attribute with ASP MVC and Castle Windsor
body: <p>I need some help - I am trying to use a custom validation attribute in an ASP.NET MVC web project that needs to make a database call.</p>
  
  <p>I have windsor successfully working for the controllers and the IRepository interface is injected normally. The problem arrises when I need to inject the repository into the attribute class. </p>
  
  <p>The attribute class has the following code:</p>
  
  <pre><code>public class ValidateUniqueUrlNodeAttribute : AbstractValidationAttribute
  {
      private readonly string message;
      private readonly IArticleRepository articleRepository;
  
      public ValidateUniqueUrlNodeAttribute(string message)
      {
          this.message = message;
      }
  
      public ValidateUniqueUrlNodeAttribute(string message, IArticleRepository articleRepository):this(message)
      {
          this.articleRepository = articleRepository;
      }
      public override IValidator Build()
      {
          var validator = new UniqueUrlNodeValidator(articleRepository) { ErrorMessage = message };
  
          ConfigureValidatorMessage(validator);
  
          return validator;
      }
  </code></pre>
  
  <p>My problem is that I cannot seem to make Windsor intercept the contruction of the attribute to pass in the IArticleRepository </p>
  
  <p>The current code in my global.asax file is as follows:</p>
  
  <pre><code>container = new WindsorContainer();
  ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(Container));
     container
       .RegisterControllers(Assembly.GetExecutingAssembly())
       .AddComponent&lt;IArticleRepository, ArticleRepository&gt;()
       .AddComponent&lt;ValidateUniqueUrlNodeAttribute&gt;();
  </code></pre>
  
  <p>Any help would be greatly appreciated. </p>