title: FluentValidation validation factory and Ninject DI container
body: <p>I'm using <code>NinjectHttpApplication</code> with couple modules defined in my project. </p>
  
  <p>What I want is to create <code>FluentValidation</code> validation factory as described in <a href="http://www.thekip.nl/2011/09/22/using-fluentvalidation-for-both-domain-validation-and-validation-in-mvc-projects/" rel="nofollow">http://www.thekip.nl/2011/09/22/using-fluentvalidation-for-both-domain-validation-and-validation-in-mvc-projects/</a>. </p>
  
  <p>To create a concrete validation factory I need to override </p>
  
  <pre><code>IValidator CreateInstance(Type validatorType) 
  </code></pre>
  
  <p>method where I should then call </p>
  
  <pre><code>return kernel.Get&lt;validatorType&gt;() as IValidator
  </code></pre>
  
  <p>But I've read that using IKernel outside of <code>Global.asax</code> scope is not recommended.</p>
  
  <p>What options are there to make what I want?</p>
  
  <p><strong>EDIT: using Ninject-FluentValidation extension</strong></p>
  
  <p>As Remo's stated there's an extension on <code>GitHub</code> (<a href="https://github.com/ninject/ninject.web.mvc.fluentvalidation" rel="nofollow">https://github.com/ninject/ninject.web.mvc.fluentvalidation</a>). There's a class in the extension:</p>
  
  <pre><code>public class NinjectValidatorFactory : ValidatorFactoryBase { ... }
  </code></pre>
  
  <p>which takes <code>IKernel</code> in constructor and creates instances of <code>IValidator</code></p>
  
  <pre><code>public override IValidator CreateInstance(Type validatorType)
  {
      if(((IList&lt;IBinding&gt;)Kernel.GetBindings(validatorType)).Count == 0)
      {
          return null;
      }
  
      return Kernel.Get(validatorType) as IValidator;
  }
  </code></pre>
  
  <p>then my code goes like:</p>
  
  <pre><code>public class MvcApplication : NinjectHttpApplication
  {
      private NinjectValidatorFactory nvfactory;
  
      public static void RegisterGlobalFilters(GlobalFilterCollection filters)
      {
          filters.Add(new HandleErrorAttribute());                        
      }
      public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
          routes.MapRoute(
              "Default",
              "{controller}/{action}/{id}",
              new { controller = "Employee", action = "Index", id = UrlParameter.Optional }
          );
      }        
      protected override void OnApplicationStarted()
      {
          AreaRegistration.RegisterAllAreas();
          RegisterRoutes(RouteTable.Routes);
  
          ModelValidatorProviders.Providers.Clear();
          ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(nvfactory));            
      }
      protected override IKernel CreateKernel()
      {
          var kernel = new StandardKernel();
          kernel.Load(Assembly.GetExecutingAssembly());            
          nvfactory = new NinjectValidatorFactory(kernel);
  
          return kernel;
      }
  }
  </code></pre>
  
  <p>That works. I don't know could it be resolved better though. Also I don't understand the need to expose <code>IKernel</code> as a public property on <code>NinjectValidationFactory</code>.</p>