我的情况下,它是Ninject 2我在使用IoC时如何管理对象处理?
// normal explicit dispose
using (var dc = new EFContext)
{
}
但有时我需要保持上下文较长或函数调用之间。 所以我想通过IoC范围来控制这种行为。
// 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>())
{
}
样品作用域
Container.Bind<IContext>().To<EFContext>().InSingletonScope();
// OR
Container.Bind<IContext>().To<EFContext>().InRequestScope();
重复:http://stackoverflow.com/questions/987761/how-do-you-reconcile-idisposable-and-ioc – TrueWill 2010-04-09 21:14:15
感谢您的链接 – Aval 2010-04-09 21:48:37