1
votes

Is there a way, using NInject conventions, to bind a generic interface that has a different number of type parameters than the concrete class that implements it?

For example:

    public interface IRepository<T1, T2>
    {
        ...
    }
    public class Repository<T1, T2, T3> : IRepository<T1, T2>
    {
        ...
    }
1
And how should Ninject guess that T3 argument when the resolver only supplies T1, and T2. How would you do this by hand?Steven
Bind(typeof(IRepository<C1,C2>)).To(typeof(Repository<C1,C2,C3>));Eric Gurney
Try to do this: To (typeof (Repository <,, C3>)). This will unfortunately nor compile.Steven

1 Answers

0
votes

There is no way you can do this directly, but the solution is rather simple: create a new type with two type arguments that inherits from Repository<T1, T2, T3>, like this:

class Rep<T1, T2> : Repository<T1, T2, ActualType>
{
}

This type can be mapped directly to a IRepository<T1, T2> without leaving any unresolved generic type arguments behind.