title: Generic covariance with Simple Injector and Fluent Validation
body: <p>I'm building a query pipeline (using the decorator pattern for an IQueryHandler) in which, before the query is actually executed, a number of cross-cutting concerns is handled. One of these concerns is validation, for which I'm using the Fluent Validation Library.</p>
  
  <p>Simple Injector is the IoC container of choice and I use it to register every IQueryValidationHandler using generic contravariance and the following registration:</p>
  
  <pre><code>container.RegisterManyForOpenGeneric(typeof(IQueryValidationHandler&lt;&gt;), container.RegisterAll, _assemblies);
  </code></pre>
  
  <p>This works perfectly for resolving the following implementation:</p>
  
  <pre><code>public class QueryValidationHandler : IQueryValidationHandler&lt;IQuery&gt;{ }
  </code></pre>
  
  <p>The query available in the class is a concrete type of IQuery. However, when I try to inject an IValidator (through constructor-injection) based on IQuery into the QueryValidationHandler, using the following code and registration:</p>
  
  <p><em>Code:</em></p>
  
  <pre><code>public class QueryValidationHandler : IQueryValidationHandler&lt;IQuery&gt;
  {
      private IValidator&lt; IQuery &gt; _validator;
  
      public QueryValidationHandler(IValidator&lt;IQuery&gt; validator)
      {
          _validator = validator;
      }
  </code></pre>
  
  <p><em>Registration</em>:</p>
  
  <pre><code>container.RegisterManyForOpenGeneric(typeof (IValidator&lt;&gt;), container.RegisterAll, _assemblies);
  </code></pre>
  
  <p>I receive the following error:</p>
  
  <blockquote>
    <p>The constructor of type QueryValidationHandler contains the parameter of type IValidator&lt;IQuery> with name 'validator' that is not registered. Please ensure IValidator&lt;IQuery> is registered in the container, or change the constructor of QueryValidationHandler.</p>
  </blockquote>
  
  <p>Is there a way to get an implementation for IValidator for the concrete type of IQuery that's injected into the QueryValidationHandler?</p>
  
  <p><strong>Update</strong></p>
  
  <p>After Steven's excellent answer I am still stuck (with same exception). Most likely because I'm trying to register implementations of Fluent Validators AbstractValidator&lt; T > as IValidator&lt; T >. In previous projects I was able to do the following registration:</p>
  
  <pre><code>container.RegisterManyForOpenGeneric(typeof(IValidator&lt;&gt;), typeof(FindProductsForCompanyQuery).Assembly);
  </code></pre>
  
  <p>For the following implementations:</p>
  
  <pre><code>public class FindProductsForCompanyQueryValidator : AbstractValidator&lt;FindProductsForCompanyQuery&gt;
  {
      public FindProductsForCompanyQueryValidator()
      {
          RuleFor(q =&gt; q.CompanyId).NotNull();
      }
  }
  </code></pre>
  
  <p>The QueryValidationHandler mentioned above should be injected with all the AbstractValidator implementations in the assembly, so it can check for errors and throw an exception if validation fails.</p>