加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

依赖注入 – 当我使用IoC时,如何管理对象处理?

发布时间:2020-12-14 04:56:10 所属栏目:百科 来源:网络整理
导读:我的情况是Ninject 2. // normal explicit disposeusing (var dc = new EFContext) {} 但有时我需要保持上下文更长时间或在函数调用之间. 所以我想通过IoC范围来控制这种行为. // if i use this way. how do i make sure object is disposed.var dc = ninjec
我的情况是Ninject 2.

// 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();

解决方法

据我所知(我在一个月前做了一项研究)Ninject根本不支持生命周期管理. Castle Windsor和AutoFac(以及某种程度上的StructureMap,但仅在使用嵌套容器时)将负责处理他们在适当时间创建的一次性组件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读