title: How can I automatically register all my fluent validators with Unity?
body: <p>Right now I've got my validators hooked up and building in my app, but every time we add a new validator we need to manually go into our Unity configuration and register the type. I'd like to do this automatically, much like <a href="http://www.jeremyskinner.co.uk/2010/02/22/using-fluentvalidation-with-an-ioc-container/" rel="noreferrer">this blog post describes doing with StructureMap</a>, only for Unity instead.</p>
  
  <p>Right now I've got something like this:</p>
  
  <pre><code>    // in global.asax.cs
      protected void Application_Start(Object sender, EventArgs e)
      {
          // some irrelevant registrations (area registrations, route config, etc)
  
          var container = new UnityContainer();
          UnityConfig.RegisterComponents(container);
          FluentValidationModelValidatorProvider.Configure(c =&gt; c.ValidatorFactory = new UnityValidatorFactory(container));
      }
  
  public class UnityValidatorFactory : ValidatorFactoryBase
  {
      private readonly IUnityContainer container;
  
      public UnityValidatorFactory(IUnityContainer container)
      {
          this.container = container;
      }
  
      public override IValidator CreateInstance(Type validatorType)
      {
          if (container.IsRegistered(validatorType))
          {
              return container.Resolve(validatorType) as IValidator;
          }
  
          return null;
      }
  }
  
  public static class UnityConfig
  {
      public static void RegisterComponents(IUnityContainer container)
      {
          container.RegisterTypes(
              AllClasses.FromAssemblies(
                  Assembly.GetAssembly(typeof (UnityConfig)),
              WithMappings.FromMatchingInterface, WithName.Default);
  
          RegisterValidators(container);
  
          DependencyResolver.SetResolver(new UnityDependencyResolver(container));
      }
  
      private static void RegisterValidators(IUnityContainer container)
      {
          container.RegisterType&lt;IValidator&lt;MyFirstViewModel&gt;, MyFirstViewModelValidator&gt;();           
          container.RegisterType&lt;IValidator&lt;MySecondViewModel&gt;, MySecondViewModelValidator&gt;();                                   
      }
   }
  </code></pre>
  
  <p>What I have is working, but I have to keep adding registrations to RegisterValidators() every time a new validator is created. Is there a way I can set this up to automatically detect and register all validators?</p>