Skip to content
bartelink edited this page Aug 1, 2013 · 9 revisions

This is basically the same as InTransientScope() insofar as it comes to the lifecycle and the decision of which object is injected. As with InTransientScope, a new instance is injected for each dependency. The difference to InTransientScope is that instances emanating from bindings with this scope will be deactivated after the object that gets the instance injected is deactivated.

public class Foo
{
    public Foo(Bar bar) { ... }
}
 
public class Bar : IDisposable { ... }
 
Bind<Bar>().ToSelf().InParentScope();

In other words when the parenting object is collected by the garbage collector (or preemptively released by a lifetime management system such as Ninject.Web.Mvc which Kernel.Releases at the End of a Request), the container automatically Disposes the object as part of the same cleanup work.

In this example, the Bar instance gets Disposed by Ninject as soon as the parent Foo instance gets collected by the garbage collector (or actively released e.g. by per-call Releasing of a scoping object).

Related

Clone this wiki locally