Ninject 缓存注入的 DataContext?生命周期管理?
我的存储库中出现了一系列非常奇怪的错误。未找到或更改行,2 次更新中有 1 次失败...没有任何意义。
就好像我的 DataContext 实例被缓存了......没有任何意义,我正在考虑职业变动。
然后我注意到 DataContext 实例是使用依赖注入、使用 Ninject 传入的(这是我第一次使用 DI...)。我删除了依赖注入,一切都恢复正常。即刻。
所以依赖注入是问题所在,但我仍然不知道为什么。我推测 Ninject 正在缓存注入的 DataContext。
这是正确的吗?
编辑:
Ninject 绑定如下:
Bind<IPupilBlockService>().To<SqlPupilBlockService>()
.WithConstructorArgument("db", new dbDataContext());
I had a series of very bizarre errors being thrown in my repositories. Row not found or changed, 1 of 2 updates failed... Nothing made sense.
It was as if my DataContext instance was being cached... Nothing made sense and I was considering a career move.
I then noticed that the DataContext instance was passed in using dependency injection, using Ninject (this is the first time I have used DI...). I ripped out the Dependency Injection, and all went back to normal. Instantly.
So dependency injection was the issue, but I still don't know why. I am speculating that Ninject was caching the injected DataContext.
Is this correct?
Edit:
The Ninject binding is as follows:
Bind<IPupilBlockService>().To<SqlPupilBlockService>()
.WithConstructorArgument("db", new dbDataContext());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于任何生命周期必须显式管理的对象(例如实现 IDisposable 的对象)或对用户很重要的对象,请尝试不要注入它们,而是注入一个允许创建此类对象的工厂。例如定义这个接口:
并按如下方式使用它:
的实现将非常简单,如下所示:
注册过程如下:
工厂的使用使得非常明确谁是所创建对象的所有者以及谁应该控制它的寿命。这使您的代码更具可读性,并遵循最小惊喜原则。
更新
自从我提交这个答案以来已经过去一年多了。我现在经常注入数据上下文本身,并根据每个(网络)请求进行注册,而不是使用工厂。然而;与往常一样,您可能需要改变应用程序的设计方式:这取决于情况。请查看此答案。
For any object which lifetime must explicitly managed (such as objects that implement
IDisposable
) or matters to the user, try not to inject them, but inject a factory that allows creating such objects instead. Define this interface for instance:And use it as follows:
An implementation of would be very simple, like this:
And registration goes like this:
The use of a factory makes it very explicit who is the owner of the created object and who should control its lifetime. This makes your code more readable and follows the principle of least surprise.
UPDATE
More than a year has past since I submitted this answer. Instead of using factories, I now often inject the data context itself, and register it on a per (web) request basis. However; a shift in how you need to design your application might be needed, so as always: it depends. Please take a look at this answer.
@Steven 给出了很好的解释,但实际上,Ninject 已经为您提供了一种指定为每个请求生成实例的方法: InRequestScope。
@Steven gave a great explanation, but actually, Ninject already gives you a way to specify that an instance be generated for per-request: InRequestScope.
查看激活行为。您的 DataContext 可能被绑定在 Singleton 作用域中,或者其他作用域之一,这将导致从内核返回相同的实例,具体取决于您的调用上下文。
(我对 ninject 不太熟悉,但给定瞬态范围是默认值,我希望在您的应用程序代码中的某个位置 DataContext 被明确定义为具有非瞬态范围)
Take a look at Activation Behaviours. It's possible your DataContext is being bound in a Singleton scope, or one of the other scopes that will result in the same instance being returned from the kernel depending on your call context.
(I'm not too familiar with ninject but given transient scope is the default, I'd expect somewhere in your app code the DataContext is explicitly defined as having a non-transient scope)