开发者

Nested UI requiring nested transactions in ActiveRecord

I have a dialog D1 which edits model type M1, and another dialog D2 which edits model type M2. One o开发者_JAVA技巧f the things that M2 contains is a reference to an M1, and so as a convenience to the user D2 contains a button that launches D1, in addition to both D1 and D2 being accessible from the top level.

Each of D1 and D2 create a TransactionScope (using TransactionMode.New) on entry, modifies the model objects (M1 or M2) as the user interacts with the dialog, and commit/rollback as appropriate when the user presses the OK/Cancel buttons.

This works fine in isolation, when the dialogs are opened from the top level.

When D1 is opened from within D2, the expected behaviour is that clicking OK in D1 should immediately save to the database and the changes in M1 should then become visible to D2. (D2 can then save or cancel its own changes to M2 without affecting M1.)

What's actually happening is that the changes to M1 do seem to be getting saved to the database when D1 closes (and before D2 closes), but D2 can't see the changes to M1 -- presumably because the session/transaction in D2 thinks that it has the up-to-date objects so doesn't requery the database, even when FindFirst etc get called. Is there some way to force it (without losing the changes to M2)?

(Another weird behaviour is that both D1 & D2 hook on to TransactionScope.OnCompleted, but this only fires on D1 when D2 is closed for the case when D1 is called from D2.)


Ok, I think I've found something which works, but it's a bit ugly, so I'm still interested in any better answers.

The trick was to modify D1 such that when it's being called nested, after committing its own transaction (and thereby returning to D2's transaction scope), to re-Find the objects it just modified and Refresh them.

And it can detect that it's in this nested scenario because OnCompleted didn't get called when it Disposed its own transaction.

(One of the problems with this solution is that it presumably wouldn't work for a three-layer nesting scenario, unless the innermost layer could pass its list of modified objects all the way to the outermost layer, which gets really ugly. Fortunately in my case I only needed two layers.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜