What is the point to bind Ninject Kernel ToConstant and use InTransientScope?
Asked Answered
F

1

6

I want to bind IServiceProvider to Ninject IKernel implementation. What is the point to use

Bind<IKernel>().ToConstant(this).InTransientScope();

binding from Ninject sources ?

This is the way how Ninject bind IKernel to KernelBase implementation. I can't understand the point. ToConstant binding type set scope to Singleton implicitly. And TransientScope with ToConstant binding type does not give any sense to me.

Footer answered 16/1, 2015 at 12:43 Comment(0)
D
12

ToConstant has two primary effects:

  • ninject keeps a strong reference to the "constant" through the whole lifetime of the kernel
  • the scope is configured to Singleton scope.

Now the call to .InTransientScope() after ToConstant changes the scope from Singleton back to transient.

What does this change? "transient" objects will not be disposed by ninject. For Singleton objects, if they're disposable, ninject will dispose them when the kernel is disposed. It's not doing that for "transient" objects.

So if the kernel would be bound .InSingletonScope(), doing kernel.Dispose() would result in another call to kernel.Dispose() - maybe even a Stack Overflow.

Doable answered 17/1, 2015 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.