How do I manage disposing an object when I use IoC?
Posted
by Aval
on Stack Overflow
See other posts from Stack Overflow
or by Aval
Published on 2010-04-09T19:10:08Z
Indexed on
2010/04/09
19:13 UTC
Read the original article
Hit count: 469
How do I manage disposing an object when I use IoC? My case it is Ninject.
// normal explicit dispose
using (var dc = new EFContext)
{
}
But sometimes I need to keep the context longer or between function calls. So I want to control this behavior through IoC scope.
// if i use this way. how do i make sure object is disposed.
var dc = ninject.Get<IContext>()
// i cannot use this since the scope can change to singleton. right ??
using (var dc = ninject.Get<IContext>())
{
}
Sample scopes
Container.Bind<IContext>().To<EFContext>().InSingletonScope();
Container.Bind<IContext>().To<EFContext>().InRequestScope();
© Stack Overflow or respective owner