1

I have been reading other questions and answers but I can't quite figure out the answer to my solution.

I have the following scenario:

class BaseA {}

class BaseB {}

IInterface<TClassA, TClassB> 
    where TClassA : BaseA
    where TClassB : BaseB

IterfaceImplementation<TClassA, TClassB> :IInterface<TClassA, TClassB> 
    where TClassA : BaseA
    where TClassB : BaseB

class A : BaseA {}

class B : BaseB {}

Note: BaseA and BaseB do not implement interfaces.

With this setup what I need to do in order for Ninject to bind

 IIntreface<A,B>

to

 InterfaceImplementation<A,B>

I have to have a binding

 kernel.Bind<IInterface<A,B>>().To<InterfaceImplementation<A,B>>()

However, I am trying to avoid this because of multiple implementations of BaseA and BaseB and do not want to have to do additional bindings for each implementation. What I want to do is

Bind<IInterface<Any(BaseA), Any(BaseB>>().To<InterfaceImplementation<ThatSame(A), ThatSame(B)>>();

Is there any way to this?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
veili_13
  • 63
  • 1
  • 9

1 Answers1

1

Specifying open generics always works the same way in .NET:

kernel.Bind(typeof(IInterface<,>)).To(typeof(InterfaceImplementation<,>));

Which will bind it so any interface closing types will be used for the implementation. The comma can be used as a separator to specify the number of open generic parameters.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Okey, so this means any IInterface can be bind to InterfaceImplementation and the constraints on the interface generic types should be caught by the compiler if someone tries to bind to classes not derived from BaseA and BaseB, correct ? – veili_13 Mar 21 '18 at 19:09
  • It will register all implementations. When you specify `IInterface` as a constructor parameter, it will resolve only if there is such an implementation registered as `InterfaceImplementation`. – NightOwl888 Mar 21 '18 at 19:13
  • So if one decides to add a specific implementation for `IInterface`, for instacce `MyImplementation : IInterface`, can it the be bind to that specific implementation with static generics for instace `(Re)bind().To();` ? – veili_13 Mar 21 '18 at 19:19
  • 1
    Yes, you can make additional registrations besides this one, but you will need to add one registration for each type if you go that route. Alternatively, you could use some [Reflection code](https://stackoverflow.com/a/48852679) to find and register them all. – NightOwl888 Mar 21 '18 at 19:27
  • Thanks a lot! My goal was to have a base class that implements logic for each derived class and the additional binding was in case an implementation wants to deviate from the base logic, in that case I want a specific binding. Still thank you for the suggestions will look more into open generics and their bindings and reflection bindings as well. – veili_13 Mar 21 '18 at 19:30
  • I might have misunderstood, regarding your second comment. This means I can have either one binding for all derived types, or I have to bind each type i.e. I cannot override (rebind) the open generic binding? – veili_13 Mar 23 '18 at 12:18