Ninject和DataContext处理 [英] Ninject and DataContext disposal

查看:76
本文介绍了Ninject和DataContext处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ninject从内核中检索我的DataContext,我想知道Ninject是否自动释放DataContext,或者他如何处理dispose()行为.根据我自己的经验,我知道处理数据上下文非常重要,并且每当您创建DataContext的直接对象(如new DataContext())时,都应该使用using()块.

I'm using Ninject to retrieve my DataContext from the kernel and I was wondering if Ninject automatically disposes the DataContext, or how he handles the dispose() behaviour. From own experiences I know disposing the datacontext is pretty important and that whenever you create a direct object of the DataContext (as in: new DataContext()) you should use a using() block.

因此,我的问题是:从内核中检索我的DataContext时,是否还必须使用using()块?还是Ninject为我解决了这个问题?

My question thus is: When im retrieving my DataContext from the kernel, should I still have to use a using() block? Or does Ninject fix this for me?

推荐答案

我正在为我的同事巴斯进行调查.我正在查看Ninject 2源代码( http://github.com/ninject/ninject.git ),并且似乎存在一些生命周期管理.

I am investigating this for my colleague Bas. I was looking in the Ninject 2 source code (http://github.com/ninject/ninject.git), and there seems to be some life cycle management.

当存在除临时范围之外的其他范围时,该范围的垃圾回收将触发(通过管道)与该范围链接的所有实例的停用.停用时,默认策略之一是DisposableStrategy,如果IDisposable,则该策略将处置该实例!

When there's an other scope than transient, then the garbage collection of that scope will trigger the deactivation (through the pipeline) of all instances linked to that scope. On deactivation one of the default strategies is DisposableStrategy, this strategy will dispose the instance if it's IDisposable!

我在stackoveflow上看到了很多答案,即Ninject不执行任何生命周期管理,也许对于以前的Ninject版本是正确的吗?

I saw a lot of answers on stackoveflow that Ninject doesn't do any life cycle management, maybe this was true for a previous version of Ninject?

但是这种行为非常棘手,因为在使用注入服务时,您并不了解范围.因此,您不知道是否必须自行处置该对象(瞬态),否则Ninject会处理此问题.

But this behaviour is quite tricky, since when using injected services you are not aware of the scope. Because of that you don't know if you have to dispose the object yourself (transient) or that Ninject will take care of this.

更改服务范围可能会导致错误.

Changes to the scope of a service could therefor introduce bugs.

这篇关于Ninject和DataContext处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆