I have searched a lot about these question, here and a lot of other places, but not getting everything I want to know!
InTransientScope
objects Created? In the Ninject docs it is stated that such objects are created whenever requested, but in a web api project that handles HTTP requests, the instance is created at the request start time so in this regard it is the same as InRequestScope
then? In a WebApi project, is it okay to use InTransientScope
objects knowing that they will never be kept track of by Ninject? If Ninject never keeps track of Transient objects, then what is the purpose of this scope and what happens to such objects after they have been used?
If I declare an object with InRequestScope and that object doesn't implement the IDisposable
interface, what happens to such object after the web request has completed? Will it be treated the same way as an InTransientScope
object?
Are different scopes to be used for: WebApi controllers, Repositories(that use a InRequestScope
Session that is created separately) and Application services?
There's two purposes for scopes:
As said, the disposal is optional. If it doesn't implement the IDisposable
interface it's not being disposed. There's plenty of usecases for that.
The InTransientScope
is the default scope - the one being used if you don't specify another one. It means that every time a type A
is requested from the kernel one activation takes place and the result is returned. The activation logic is specified by the binding part that follows immediately after the Bind
part (To<...>
, ToMethod(...)
,...).
However, this is not necessarily at the time the web-request starts and the controller is instanciated. For example, you can use factories or service location (p.Ex. ResolutionRoot.Get<Foo>()
) to create more objects after the controller has been created. To answer your questions in short:
IResolutionRoot.Get(..)
) or through a factory. As InTransientScope
objects are not being tracked they will not be disposed, however, if they are not disposable and the entire request code requests only one IFoo
then practically there's is no discernible difference (apart from the slight performance hit due totracking InRequestScope()
-ed objects)new
yourself.IDisposable
object. The object itself will not be touched - just like when bound InTransientScope()
InTransientScope()
unless there's a specific reason why it should not be. The reason will dictate what scope to use...