24 July 2008

How To: Declare undoable data

In this topic I will show the basis of Dejavu: declaring data member that can be subject of undo/redo.

There are many kinds of data: primitive types, references to complex types (objects), collections. All these data can be made "undoable".

Lets start from primitive type. That's how you can declare int property in your class:

using Dejavu;
...
class MyClass
{
private readonly UndoRedo<int> count = new UndoRedo<int>(5);
public int Count
{
get { return count.Value; }
set { cound.Value = value; }
}
}


As you see there are minor internal differences from classic property declaration. Public contract does not change. So, nothing changed for external developer. But the same time, we have got undoable property declaration!

Reference data type can be declared very similar:

using Dejavu;
...
class MyClass2
{
private readonly UndoRedo<MyClass> myClass = new UndoRedo<MyClass>();
public int MyClass
{
get { return myClass.Value; }
set { myClass.Value = value; }
}
}


The initial value contained in myClass will be null because we used contsructor with no parameters. In the previous code snippet our int property will be preset with initial value 5.

In next topic I will show how to get undoable collections.

No comments: