Life cycle management in Ninject
Configuration of object’s life-time in Ninject is set by invocation of appropriate method as it’s shown below:
kernel.Bind<IFileSystem>().To<FileSystem>()
.InSingletonScope();
List of predefined methods:
InScope
- delegate Func, which defines life time of an object, is passed to this method. Until the object returned by delegate is not disposed and the same one, the same instance of the object will be used. It is most common method and the other methods use it.InTransientScope
- new instance is created for each request. It is the same asInScope(c => null)
.InThreadScope
- new instance is created per each thread. It is the same asInScope(c => System.Threading.Thread.CurrentThread)
InSingletonScope
- the name speaks for itself. It is the same asInScope(c=> c.Kernel)
InRequestScope
- new instance is created for each HttpContext. The same asInScope(c => System.Web.HttpContext.Current)