DataContext.SubmitChanges时防止递归() [英] Preventing recursion during DataContext.SubmitChanges()

查看:162
本文介绍了DataContext.SubmitChanges时防止递归()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有点耍大牌,我希望你会发现这个具有挑战性的问题是有趣的,因为我的工作...:)



我有一个子类的的DataContext 名为 MyDataContext ,其中我已经覆盖了的SubmitChanges()法形式的一些代码:

 的BeginTransaction(); //我自己的实现
&IList的LT;对象>更新= GetChangeSet()更新。
的foreach(在更新obj对象){
MyClass的MC = OBJ为MyClass的;
如果(!MC = NULL)
mc.BeforeUpdate();在MyClass的虚拟//方法,使预节省处理
}
//其次是为删除和插入,那么类似的代码:
base.SubmitChanges();
//然后做后期处理保存...
的foreach(在更新obj对象){
MyClass的MC = OBJ为MyClass的;
如果(!MC = NULL)
mc.AfterUpdate();在MyClass的虚拟//方法允许后节省处理
}
//类似的代码进行插入和删除
// ...
CommitTransaction();
//显然全部封闭在一个try-catch块,其中抓执行回退



到现在为止还挺好。但是有一个小问题,这些表面如果 MyClass的调用的实现的SubmitChanges()更新前()更新后()方法。现在我们有一个递归是容易导致堆栈溢出。



我想到了要解决的一个方法是在开始时有一个递归阻断变量的的SubmitChanges()。但是做什么,如果保存被阻止?我不能转动它关闭到一个新的线程;调用线程可以要求的SubmitChanges()调用是同步的,例如如果之后立即需要访问一个自动号码属性保存



考虑的另一个因素是,如果任何对象的预过程中改变或后期处理,我也想的及其 BeforeSave() AfterSave()方法被调用。



有没有做这一切整齐,适当的一些聪明的教科书的方式?


解决方案

一个办法,我想到了变通
这是必须在
的SubmitChanges年初递归阻断
变量() 。但是,如果
保存堵塞怎么办?




抛出一个引发NotSupportedException 。您不应该支持其他的SubmitChanges 发生在BeforeChanges ......正是这就是你在做什么,允许一些变化发生的SubmitChanges被调用之前。



关于更新的对象越来越称他们更新前,您可以检查是否只是你在最初的名单称为更新前,做了之后才的SubmitChanges之前有新更新的对象有没有多余的更新对象。



这同样适用于更新后,类似的东西是要做到这一点发生在内存中对象的变化......不要节省更多数据到数据库。



试着在你的系统中的不同实体添加的SubmitChanges,是什么,势必会在系统中创建一些性能问题。


This is a bit of a poser, and I hope you'll find this challenging problem as interesting as I do... :)

I have a subclass of DataContext called MyDataContext, wherein I have overridden the SubmitChanges() method with some code of the form:

BeginTransaction(); // my own implementation
IList<object> Updates = GetChangeSet().Updates;
foreach (object obj in Updates) {
  MyClass mc = obj as MyClass;
  if (mc != null)
    mc.BeforeUpdate(); // virtual method in MyClass to allow pre-save processing
}
// This is followed by similar code for the Deletes and Inserts, then:
base.SubmitChanges();
// Then do post-save processing...
foreach (object obj in Updates) {
  MyClass mc = obj as MyClass;
  if (mc != null)
    mc.AfterUpdate(); // virtual method in MyClass to allow post-save processing
}
// similar code for Inserts and Deletes
// ...
CommitTransaction();
// obviously all enclosed in a try-catch block where the catch does a rollback

So far, so good. But there's a little problem, which surfaces if an implementation of MyClass invokes SubmitChanges() in its BeforeUpdate() or AfterUpdate() method. Now we have a recursion that is liable to lead to a stack overflow.

One way I thought of to work around this is to have a recursion-blocking variable at the beginning of SubmitChanges(). But what to do if the save is blocked? I can't spin it off into a new thread; the calling thread may require the SubmitChanges() call to be synchronous, e.g. if it needs access to an auto-number property immediately after the save.

An additional factor to consider is that if any objects are changed during the course of the pre- or post-processing, I also want their BeforeSave() and AfterSave() methods to be called.

Is there some clever textbook way of doing this all neatly and properly?

解决方案

One way I thought of to work around this is to have a recursion-blocking variable at the beginning of SubmitChanges(). But what to do if the save is blocked?

Throw a NotSupportedException. You shouldn't support another SubmitChanges happening on BeforeChanges ... precisely that's what you are doing, allowing some changes to happen Before SubmitChanges gets called.

Regarding the updated objects getting their BeforeUpdate called, you can check if there are new updated objects just before the SubmitChanges after you have called BeforeUpdate in the original list and do that until there are no extra updated objects.

The same goes for AfterUpdate, something like that is to do changes that happen to the in-memory objects ... not to save more data to the db.

Trying to add SubmitChanges on the different entities in your system, is something that is bound to create some performance issues in your system.

这篇关于DataContext.SubmitChanges时防止递归()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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