How do I manage object disposal 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
22:03 UTC
Read the original article
Hit count: 329
My case it is Ninject 2.
// 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();
// OR
Container.Bind<IContext>().To<EFContext>().InRequestScope();
© Stack Overflow or respective owner