Search code examples
c#c#-4.0dependency-injectioncastle-windsorcastle

Castle windsor: Register generics with more than one items


I have a generic interface defined like this -

public interface IGenericRepository<TEntity, TDbContextType>
    where TEntity : class
    where TDbContextType : IDbContextType 

This interface is implemented by a class like this -

 public class GenericRepository<TEntity,TDbContextType> 
    : IGenericRepository<TEntity, TDbContextType> 
    where TEntity : class 
    where TDbContextType: IDbContextType

I tried the following for registering this interface and implementation with castle -

   _container.Register(Component.For(typeof (IGenericRepository<>))
       .ImplementedBy(typeof (GenericRepository<>))
       .LifestylePerWcfOperation());

But it fails at compile time Saying "incorrect number of paramters".


Solution

  • It fails to compile because you specific generic types with one single parameter, but you defined types with two parameters.

    So you should use IGenericRepository<,> and GenericRepository<,> instead of IGenericRepository<> and GenericRepository<>.