Windows Support Number

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 22 February 2012

undo-redo-undo-redo...

Posted on 06:19 by Unknown
Code available on GitHub

I'm currently building a UI with a semi-complex input-form that requires the ability to 'undo, redo, undo, redo' user actions ad-infinitum. Not only does it require the ability to undo the text typed in text-boxes etc it needs the ability to undo (or redo) button clicks that add\remove complex properties (reference types).

For example imagine I have the following model, where there is a hierarchical structure to the data  - parent-child relationships, the Widget has one settable value type property - 'Name' & a collection of child Widgets accessed via the 'Children' property, this property can not set by the caller- the caller has to use the 'AddChild' & 'RemoveChild' methods to affect the enumerable 'Children' property. I want the ability to 'undo, redo, undo, redo' on these two properties.
public class Widget : Model
{
private int? id;
private Widget parent;
private string name;
private readonly ObservableCollection<Widget> children;

public Widget()
{
children = new ObservableCollection<Widget>();
}

public int? Id
{
get { return id; }
private set
{
SetPropertyAndNotify(ref id, value, () => Id);
}
}

public string Name
{
get { return name; }
set
{
SetPropertyAndNotify(ref name, value, () => Name);
}
}

public Widget Parent
{
get { return parent; }
private set
{
SetPropertyAndNotify(ref parent, value, () => Parent);
}
}

public IEnumerable<Widget> Children
{
get { return children; }
}

public Widget AddChild(Widget child)
{
if (children.Contains(child))
{
return this;
}

child.Parent = this;
children.Add(child);

return this;
}

public Widget AddChild(IEnumerable<Widget> childs)
{
foreach (var child in childs)
{
AddChild(child);
}

return this;
}

public Widget RemoveChild(Widget child)
{
if (!children.Contains(child))
{
return this;
}

child.Parent = null;
children.Remove(child);

return this;
}

public Widget RemoveAllChildren()
{
foreach (var child in children.ToList())
{
child.Parent = null;
children.Remove(child);
}

return this;
}
}
I know already the kind of functionality required, an implementation of the Command Pattern, specifically the Memento Pattern. A quick Google found some interesting implementations much of which I didn't like for a couple of reason, one being no separation from UI - here or to complex (to many interfaces) - here. A couple more examples were just to old, not using modern languages features like Lambda expressions. In fact the use of Lambdas should make this a trivial exercise. The solution I want should be capable of being used within an MVC implementation without being tied to it, in fact it should know nothing about either the Model or the View. I envisage it being used from inside the Controller. This fits well with the UI I'm currently building, an MVVM implementation.

The solution I came up with has 2 classes, the first is the Memento we wish to be able to 'undo-redo'. As you can see the use of Action delegate syntax to remove any explicit knowledge of how the 'undo-redo' steps will be performed. There are 2 constructors one for when both undo & redo is supported and the other when only undo is supported:
public class Memento
{
public Action Undo { get; private set; }
public Action Redo { get; private set; }

public Memento(Action undo)
{
Undo = undo;
Redo = () => { };
}

public Memento(Action undo, Action redo)
{
Undo = undo;
Redo = redo;
}
}
The second class actual contains the Memento instances, simply it contains a pair of Stack representing the undo & redo list. When an Undo action is executed the Memento is first popped from the undo stack, executed and then pushed onto the Redo stack, and it's the reverse for a Redo action. The only other behaviour of note is when a new Undo\Redo is added, any Redo's are cleared, this is to prevent confusion when changes are being reverted to a certain point and then editing continues.
public class Undoable
{
private readonly Stack<Memento> undoStack;
private readonly Stack<Memento> redoStack;

public Undoable()
{
undoStack = new Stack<Memento>();
redoStack = new Stack<Memento>();
}

public void Add(Action undoAction, Action redoAction)
{
undoStack.Push(new Memento(undoAction, redoAction));
redoStack.Clear();
}

public void Undo()
{
if (undoStack.Count == 0)
{
return;
}

var current
= undoStack.Pop();
current
.Undo();
redoStack
.Push(current);
}

public void Redo()
{
if (redoStack.Count == 0)
{
return;
}

var current
= redoStack.Pop();
current
.Redo();
undoStack
.Push(current);
}

public void Clear()
{
redoStack
.Clear();
undoStack
.Clear();
}
}
So to round a couple of test written using MSpec. The first one shows the Undo'ing of setting a simple (value type) property on the model:
[Subject("Undoable")]
public class when_undoing_a_value_type_property_modification
{
private Establish context = () =>
{
undoable = new Undoable();
widget = new Widget {Name = OriginalName};
};

private Because of = () =>
{
undoable.Add(() => { widget.Name = OriginalName; }, () => { widget.Name = NewName; });

widget.Name = NewName;
undoable.Undo();
};

private It should_undo_setting_the_name_on_a_widget = () => widget.Name.ShouldEqual(OriginalName);

private static string OriginalName = "Original Name - " + Guid.NewGuid();
private static string NewName = "New name - " + Guid.NewGuid();
private static Widget widget;
private static Undoable undoable;
}

The second shows the Redo'ing of adding a child Widget to the parent:
[Subject("Undoable")]
public class when_redoing_a_reference_type_property_modification
{
private Establish context = () =>
{
undoable = new Undoable();
parent = new Widget { Name = "Parent Widget" };
child = new Widget { Name = "Child Widget" };
};

private Because of = () =>
{
undoable.Add(() => parent.RemoveChild(child), () => parent.AddChild(child));
parent.AddChild(child);
undoable.Undo();
undoable.Redo();
};

private It parent_widget_should_cotain_child_widget = () => parent.Children.Contains(child).ShouldEqual(true);

private It parent_widget_children_collection_should_not_be_empty = () => parent.Children.Count().ShouldNotEqual(0);

private static Widget parent;
private static Widget child;
private static Undoable undoable;
}

I've pushed this to GitHub here.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in C#, Command, Design Patterns, Development | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Unit testing Rx methods Timeout & Retry with moq
    Earlier this week I was trying to unit test an asynchronous service (Foo) which used another asynchronous service (Bar) internally and ran i...
  • Implementing a busy indicator using a visual overlay in MVVM
    This is a technique we use at work to lock the UI whilst some long running process is happening - preventing the user clicking on stuff whil...
  • StructureMap: ILifecycle
    The other day I wanted to control the scope of a service inside a web based app with semantics which didn't fit either 'HttpContextS...
  • Be careful of the culture when using Bing Maps REST API
    When developing the Bing Maps Wrapper service for the WP7Contrib we weren't aware of the importance of the instance of the CultureInfo ...
  • Considerations when building a caching mechanism for WP7Contrib.
    For anyone wanting to build a cache for an application, there are several guidelines(may be rules) you want to beware of and more than likel...
  • No GetEntryAssembly in Silverlight!
    Ran into a problem today, wanting to get the assembly that started the an application. Now this isn't a tricky problem just had to bend ...
  • Can I make a value object from an entity?
    I've built a rich domain model for a private app I'm working on, it feels right, it got the correct mix of business functionality an...
  • Auditing user actions
    I want to be able audit user actions on the domain model - I want to track when & what they've changed. Now I can do this at the cou...
  • WP7Contrib: Bing Maps REST Services Wrapper - Deep Dive
    Following on from Rich's post introducing the Bing Maps Service in the WP7Contrib I'm going to explain in more detail how we built ...
  • Using CompositeDisposable in base classes
    To help make an object eligible for collection by the GC (garbage collector) one would implement the IDisposable interface. Executing the di...

Categories

  • .Net
  • .Net 4.5
  • Abstractions
  • Advertising
  • Agile
  • Agile Courage
  • AOP
  • Async
  • automated testing
  • Azure
  • Azure IIS RESTful development
  • BDD
  • Bing Maps
  • Bounded Context
  • C#
  • C# 5.0
  • Caching
  • Chocolatey
  • CLoud
  • CodePlex
  • Coding
  • Coding Building CI Testing
  • Coding C#
  • coding C# IoC StructureMap
  • Coding Functional-Programming
  • Coding REST Knowledge
  • Coding Services
  • Coding TDD Refactoring Agile
  • Command
  • continuous testing
  • coupling
  • CultureInfo
  • DAL
  • databases
  • DDD
  • DDD Coaching
  • DDD Domain Events Auditing nHibernate
  • DDD Entities Value Objects
  • Debugging
  • Design Patterns
  • Design Patterns Databases Auditing
  • Developement
  • Development
  • Development Coding
  • Development Process
  • Development unit testing
  • Development VS 2011
  • Diagnostics
  • Disposable
  • Exceptions
  • FINDaPAD
  • FindaPad Property Rental Windows Phone 7 Mobile Devices
  • Fun Coding Duct-Tape
  • Hotfixes
  • integration testing
  • IoC
  • jasmine
  • javascript
  • Jobs Development
  • LINQ
  • marketplace
  • Mobile Devices
  • Mocking
  • MSDN Coding
  • MSpec
  • Multilingual
  • MVC
  • MVVM
  • nCrunch
  • nHbiernate Repository Pattern Criteria
  • nHibernate Auditing Design Fluent
  • nHibnerate Entities Events Listeners
  • node.js
  • nodes.js
  • Nokia
  • NoSQL RavenDB Azure Development
  • Observations
  • OO
  • ORM
  • Performance
  • Portable Class Library
  • Portable Library
  • PostSharp
  • Process
  • Rants
  • RavenDB IIS 7.5 Development
  • Reactive
  • Reactive Extension
  • Reactive Extensions
  • ReadOnlyCollections
  • Resharper
  • REST Distributed-Systems
  • REST HTTP
  • rest web
  • RESTful
  • Rx
  • Serialization
  • Silverlight
  • Silverlight Installation
  • Task
  • TDD
  • TDD IoC DI
  • TDD Mocking
  • TDD Team Observation
  • Telerik
  • testing
  • threading
  • TPL
  • UI
  • Undo-Redo
  • unit testing
  • ViewModels
  • VS 2012
  • wcf
  • web api
  • Web Services
  • web services mobile devices data
  • WebAPI
  • Windows
  • Windows 8
  • windows phone
  • Windows Phone 7
  • WP7
  • WP7 Bing Maps Development Network HTTP
  • WP7 Bing Maps Development UK Crime
  • WP7 Bing Maps Development UK Crime Clustering
  • WP7 Bing Maps Development UK Polygons Clustering Performance
  • WP7 cryptography bouncy castle
  • WP7 Cultures C#
  • WP7 feedback development app store
  • WP7 Javascript web browser
  • WP7 MSBuild
  • WP7 ORM Databases performance
  • WP7 Serialisation
  • WP7 SilverlightSerializer C#
  • WP7 sqlite performance development
  • WP7 WP7Contrib Bing Maps Development
  • WP7 WP7Contrib Bing Maps Polygon Development
  • WP7 WP7Contrib CodePlex
  • WP7 WP7Contrib CodePlex Bing Maps Development
  • WP7 WP7Contrib CodePlex ObservableCollection
  • WP7 WP7Contrib ILMerge .Net
  • WP7 WP7Contrib Phone Maps
  • WP7 WP7Contrib SilverlightSerializer C#
  • WP7Contrib
  • WP7Contrib Bing Maps WP7
  • WP7Contrib WP7 Geo-Location development C#
  • WP7Contrib WP7 HTTP Compression
  • WP7Contrib WP7 Url Development Rx
  • WP7Dev
  • WPF
  • WPF Cultures
  • WuApi
  • XAML

Blog Archive

  • ►  2013 (16)
    • ►  November (5)
    • ►  September (3)
    • ►  August (1)
    • ►  July (1)
    • ►  June (3)
    • ►  May (2)
    • ►  January (1)
  • ▼  2012 (44)
    • ►  November (2)
    • ►  October (8)
    • ►  September (5)
    • ►  August (2)
    • ►  July (4)
    • ►  June (3)
    • ►  May (1)
    • ►  April (2)
    • ►  March (13)
    • ▼  February (4)
      • Playing around with undo & redo functionality on WP7
      • undo-redo-undo-redo...
      • Trying to test strongly typed datatables with sqli...
      • An ORM would have saved time...
  • ►  2011 (52)
    • ►  December (3)
    • ►  November (5)
    • ►  October (7)
    • ►  September (7)
    • ►  August (11)
    • ►  July (4)
    • ►  May (2)
    • ►  April (1)
    • ►  March (5)
    • ►  February (3)
    • ►  January (4)
  • ►  2010 (1)
    • ►  August (1)
  • ►  2009 (32)
    • ►  December (3)
    • ►  November (7)
    • ►  October (6)
    • ►  September (11)
    • ►  April (1)
    • ►  March (4)
Powered by Blogger.

About Me

Unknown
View my complete profile