Select Page

Opentribe Memento is a C# open source library which provide all you need to manage Undo/Redo feature in your programs.

The memento pattern is a software design pattern that provides the ability to restore an object to the previous state. In its original form, discribed by the “Gang or four” in the book “Design patterns : Elements of reusable object-oriented software”, the memento design patters was implemented in three objects. It is not the case in this implementation. I can say that the Opentribe Memento library does not implement strictly the pattern designed by the “Gang of four”.
For Patrick Soquet, creator of Key language, Apple Media Tools authoring tool and iShell authoring tool, the architecture of a software is deeply influenced by the language.
Followong this idea, I created the Opentribe Memento library which doesn’t strictly follow the memento design pattern, but achieves the same target of the design pattern using elegants features of c# language.

You can access to the Opentribe Memento nuget package.
Opentribe Memento is a free open source library. Source code is available from its bitbucket dedicate page.

The example

// this is the definition of the object I want to manage
public class MyObject
{
public string MyStringAttribute { get; set; }
public int MyIntAttribute { get; set; }
}

// somewhere you have to instanciate an object
public MyObject AnObject { get; set; } = new MyObject();

How to

Initialize the Memento

// First step is to initialize a Memento object. This object is the “Caretaker” in the memento design pattern.
public static Memento.Memento Caretaker { get; set; } = new Memento.Memento();

Remember and change one attribute

To remember the state of an object, you have to ask the Caretaker to remember each attributes of the object than you are free to modify these attributes.

// remember and change 1 attribute
Caretaker.Remember<MyObject, string>(AnObject, “MyStringAttribute”); // remember

AnObject.MyStringAttribute = “A new state”; // change attribute

Caretaker.Undo(); // restore the previous value

Remember and change several attributes

To remember the state of an object, you have to ask the Caretaker to remember each attributes of the object than you are free to modify these attributes.

// remember and change attributes

Caretaker.Begin(); // start to remember
Caretaker.Remember<MyObject, string>(AnObject, “MyStringAttribute”); // remember first attribute
Caretaker.Remember<MyObject, int>(AnObject, “MyIntAttribute”); // remember second attribute
Caretaker.End(); // stop registering

AnObject.MyStringAttribute = “A new state”; // change first attribute
AnObject.MyIntAttribute = 123; // change second attribute

Caretaker.Undo(); // restores second and first attribute.

 

License

Copyright (c) 2017 Fred Morales <guru_meditation@rocketmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of mosquitto nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.