以 NerdDinner 为例,什么时候应该释放 DataContext

发布于 2024-08-12 03:48:10 字数 228 浏览 3 评论 0原文

我注意到 NerdDinner 永远不会处理 DataContext。这对我来说似乎很奇怪。他们有一个 Repository 类,它创建数据上下文的私有成员,该成员在存储库类的生命周期内一直存在 - 谁知道它有多长,因为它是在控制器构造时再次作为私有成员创建的变量并且从未取消引用。控制器何时取消引用?

我认为问题在于传递 IQueryable 类型,在您迭代它们时必须有一个开放的连接。但这是否意味着会有一些悬空的开放连接?

I noticed that NerdDinner doesn't dispose of the DataContext -- ever. That seems very strange to me. They have a Repository class that creates a private member of the data context, which hangs around for the lifetime of the repository class -- and who knows how long that is, since it's created at the time of the controller's construction again as a private member variable and never dereferenced. When is the controller dereferenced?

I believe the issue revolves around passing IQueryable types, which must have an open connection at the time you iterate over them. But doesn't this then mean that there will be some dangling open connections?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日暮斜阳 2024-08-19 03:48:10

使存储库本身成为一次性的。处置存储库时处置数据上下文。覆盖 Controller.Dispose 并在那里处置存储库。当视图执行时,控制器仍然处于活动状态。

Make the Repository itself disposable. Dispose the data context when the repository is disposed. Override Controller.Dispose and dispose the repository there. The controller is still alive when the view is executed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文