I have an application that uses Ninject for its DI. I'm adding some new classes and they are not being resolved the way I think they should be.
I have a class Foo that implements an interface ISomething. If I configure the bindings like this:
kernel.Bind<Foo>().ToSelf().InSingletonScope();
kernel.Bind<ISomething>().To<Foo>();
and then do:
var foo1 = kernel.Get<Foo>();
var foo2 = kernel.Get<ISomething>();
the two variables have different instances of the Foo class.
If I change the binding to:
kernel.Bind<Foo>().ToSelf().InSingletonScope();
kernel.Bind<ISomething>().ToMethod(ctx => ctx.Kernel.Get<Foo>());
and then do:
var foo1 = kernel.Get<Foo>();
var foo2 = kernel.Get<ISomething>();
the two variables have the same instance.
My understanding is that in the first case it should resolve the ISomething to Foo, which will in turn resolve to the singleton of itself. Is my understanding incorrect, or is something else wrong? It seems a bit redundant to have to manually resolve it.