我的情況下,它是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