以 NerdDinner 为例,什么时候应该释放 DataContext
我注意到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使存储库本身成为一次性的。处置存储库时处置数据上下文。覆盖 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.