Memento.Mementor.Undo C# (CSharp) Method

Undo() public method

Performs an undo.
public Undo ( ) : void
return void
        public void Undo()
        {
            if (!CanUndo) throw new InvalidOperationException("There is nothing to undo");
            if (IsInBatch) throw new InvalidOperationException("Finish the active batch first");

            var @event = _undoStack.Pop();
            RollbackEvent(@event is BatchEvent ? new BatchEvent((BatchEvent) @event) : @event, true);
            NotifyChange(@event);
        }

Usage Example

        public void when_attempting_to_version_a_property_bag()
        {
            var originalAddress = "12345 Fake Street";
            var subscriber = new SimpleMementoingClass()
                                 {
                                     Domainees = new List<DomainPropertyBag>()
                                                     {
                                                         new DomainPropertyBag(),
                                                         new DomainPropertyBag()
                                                     },
                                     ChiefDomainee = new DomainPropertyBag(),
                                     Address = originalAddress,
                                     FirstName = "If only Customer Objects",
                                     LastName = "were ever this simple"
                                 };

            var mementor = new Mementor();

            var newAddress = "Changed Address!";
            mementor.PropertyChange(subscriber, () => subscriber.Address);
            //ohhh typing is a little weak here.
            subscriber.Address = newAddress; //ahh, note the order matters of course.

            subscriber.Address.Should().Be(newAddress);

            mementor.Undo();
            //and I cant undo specific changes, just the last one.

            subscriber.Address.Should().Be(originalAddress);
            //still, very cool.
                //In terms of a deployed program, this is tough to implement.
                //I like the pub-sub design of it, code is superbly clean.
        }
All Usage Examples Of Memento.Mementor::Undo