LINQ到SQL - 保留的DataContext跨越后背上,? [英] Linq to SQL - Retain DataContext across post-backs?

查看:96
本文介绍了LINQ到SQL - 保留的DataContext跨越后背上,?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了里克施特拉尔的一篇关于DataContext的生命周期管理,以及一些的#2的其他相关问题。如果它们包含在回答我的问题,我一定是错过了。

I read Rick Strahl's post about DataContext lifetime management, and some of the other related questions on Stackoverflow. If they contained an answer to my question, I must have missed it.

我一般遵循原子的方法和实例化一个DataContext用于在需要时工作的单位,事后处置它。这个工作很好,直到我打的情景与包含几个网格和弹出面板,全部重新工作present一个单位多视角控制复杂的页面。该数据是在内存中(其实我的东西根对象到会话,使整个层次现已在后背上,)。很显然,在DataContext早已被用户点击保存的时候了。

I generally follow the atomic approach and instantiate a DataContext for a unit of work when it is needed, and dispose it afterwards. This worked well until I hit a scenario with a complex page that contains a multi-view control with several grids and popup panels that all represent one unit of work. The data is in memory (I actually stuff the root object into the session so that the entire hierarchy is available across post-backs). Obviously, the DataContext is long gone by the time the user clicks on "Save".

汤姆·布伦的评论吸引我的目光在第一,因为它似乎像这样一个优雅的方法 - 使用反射来湿的对象的全新副本,并更新使用新的DataContext数据库。但是,里克的担忧对这种做法是有效的,因为我的数据结构是复杂的,分层的,我不认为我会尝试。

Tom Brune's comment caught my eye at first, because it seemed like such an elegant approach - to use reflection to "wet" a fresh copy of the object and to update the database using a new DataContext. However, Rick's concerns about this approach are valid, and since my data structures are complex and hierarchical, I don't think I will try this.

所以我留下了几个选项,据我看到的。

So I am left with few options, as far as I see.


  • 要么使用Rick的建议,反序列化/序列化对象并将其重新连接到一个新的上下文

  • 手工code,用于比较和更新对象
  • 的全新副本逻辑
  • either use Rick's suggestion to deserialize/serialize the object and re-attach it to a new context
  • hand-code the logic that compares and updates a fresh copy of the object

我应该遵循哪一个,有没有第三种选择,即我可以保持在DataContext后背上,各地之间?如果这是可行的,它需要的编码量最少,因为我的根对象大约有十几个孩子。

Which one should I follow, and is there a third option, i.e. can I keep the DataContext around between post-backs? If that's feasible, it would require the least amount of coding, as my root object has about a dozen children.

推荐答案

我的建议是你的第一点也和反序列化去/序列化对象,然后将其重新连接到一个新的环境。

My suggestion would be to go with your first bullet point there and deserialize/serialize the object and then re-attach it to a new context.

我已经在过去使用这种方法,它一直很适合我。我想你会冲在最前面在较少的问题,有一个更容易实现。

I've used that approach in the past and it has worked well for me. I think you'll run in to less issues and have an easier implementation ahead.

这篇关于LINQ到SQL - 保留的DataContext跨越后背上,?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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