I am looking for a simple n-level undo example

I am looking for a simple n-level undo example

Old forum URL: forums.lhotka.net/forums/t/5654.aspx


ezimmerman posted on Wednesday, October 22, 2008


Really the only thing that I am looking to accomplish is n-level undo/redo, but having a hard time grasping how to use CSLA for that. I can understand the concepts of saving a snapshot of an object and storing it, but everything I've read says that undo/redo is "built in".

Are there any clear, simple examples for "n-level" undo/redo?

Thanks,
Eric

RockfordLhotka replied on Wednesday, October 22, 2008

There are different scenarios:

  1. No data binding
  2. Web Forms data binding
  3. Windows Forms data binding
  4. Windows Forms data binding w/ top level cancel button
  5. WPF data binding
  6. WPF data binding w/ CslaDataProvider

Scenarios 1, 2 and 5 are all pretty similar - you explicitly call the object's n-level undo methods:

Customer cust = Customer.GetCustomer(123);
cust.BeginEdit();
// interact with cust
cust.ApplyEdit(); // or cust.CancelEdit();
cust = cust.Save(); // unless you did a CancelEdit() of course

Scenarios 3 and 4 are covered in the Using CSLA .NET 3.0 ebook, and are not trivial. This is because data binding directly interacts with the object's undo features, and Windows Forms has a lot of very specfiic rules YOU MUST FOLLOW or it won't work right.

The new CslaActionExtender in version 3.6 should help simplify some aspects of scenarios 3 and 4.

Scenario 6 may be the simplest of the bunch, because you just set the ManageObjectLifetime property of the CslaDataProvider to True and let the CslaDataProvider (and WPF commanding) do nearly all the work.

In any case, the ProjectTracker reference app is the place to look for examples of all interface types.

ezimmerman replied on Wednesday, October 22, 2008


Alright, how about a specific case...

- no data binding.
- We have one object and one 'description' string property.
- The description is originally blank.

1. User changes the description to "help".
2. User changes the description to "I'm stuck".
3. User clicks undo
4. User clicks undo (we should be back to <blank>)
5. User clicks redo
6. User clicks redo. (and we should be back to "I'm stuck")

What are the proper calls for this scenario?

Thanks,
Eric

RockfordLhotka replied on Wednesday, October 22, 2008

The undo feature of CSLA doesn’t work (easily) at that level of granularity. It doesn’t record specific changes – it just takes a snapshot of the object graph’s state at a point in time, and reverts to that point.

 

So to do what you describe:

 

x.BeginEdit()

x.Description = “help”

x.BeginEdit()

x.Description = “I’m stuck”

x.CancelEdit()

Print x.Description

> “help”

x.CancelEdit()

Print x.Description

> “”

 

There is no redo supported by CSLA, so 5 and 6 won’t happen.

 

And really, BeginEdit() is an expensive operation – it isn’t something you want to do to trap a single field change – it is designed to work at the object graph level.

 

Rocky

 

ezimmerman replied on Wednesday, October 22, 2008


Thank-you for the response.
I'm disappointed that redo isn't a part of it...

Copyright (c) Marimer LLC