public void Undo() { if (!Executed) { throw new CommandNotExecutedException(); } if (!_Db.RemoveNode(_NewNode)) { throw new CommandFailedDbWriteException(); } _ParentNode.RemoveChild(_NewNode); _NewNode.RemoveParent(_ParentNode); Executed = false; }
public void Execute() { if (Executed) { throw new CommandAlreadyExecutedException(); } if (!_Db.RemoveNode(_RemoveNode)) { throw new CommandFailedDbWriteException(); } //Make note of any links between the node's parents and the node's children //We need this information to rebuild these links if we undo the command foreach (var parent in _RemoveNode.ParentNodes) { foreach (var parentChild in parent.ChildNodes) { foreach (var child in _RemoveNode.ChildNodes) { if (ReferenceEquals(parentChild, child)) { _ParentChildLinks.Add(parent, parentChild); } } } } //Link up each of the node's children to each of the node's parents foreach (var node in _RemoveNode.ChildNodes) { _Children.Add(node); foreach (var parent in _RemoveNode.ParentNodes) { node.AddParent(parent); } node.RemoveParent(_RemoveNode); } //Link up each of the node's parents to each of the node's children foreach (var parent in _RemoveNode.ParentNodes) { _Parents.Add(parent); foreach (var node in _RemoveNode.ChildNodes) { parent.AddChild(node); } parent.RemoveChild(_RemoveNode); } //Remove eacho f the node's children foreach (var node in _Children) { _RemoveNode.RemoveChild(node); } //Remove each of the node's parents foreach (var parent in _Parents) { _RemoveNode.RemoveParent(parent); } Executed = true; }