This question is locked. New answers and comments are not allowed.
I am trying to delete the connection that comes or goes from a node when the node is deleting. Using MVVM, I override the below methods in GraphSource to achieve this. However, At the time RemoveItem is triggered, the connections have already updated (Source or Target property is null).
I also don't let the connection with null Source or Target property, so removing links with null target or source will solve my problem. But I wonder if there is a better way to achieve this?
Below you can find my functions.
/// <summary>/// Gets the node./// </summary>/// <param name="id">The identifier.</param>/// <returns>Node with given id.</returns>public Node<T> GetNode(string id){ return this.InternalItems.FirstOrDefault(x => x.Entity.GetIdentifier() == id);}/// <summary>/// Adds the node./// </summary>/// <param name="node">The node.</param>public override void AddNode(Node<T> node){ if (node == null) { throw new ArgumentNullException("node"); } Node<T> foundNode = this.GetNode(node.Entity.GetIdentifier()); if (foundNode == null) { base.AddNode(node); }}/// <summary>/// Adds the link./// </summary>/// <param name="link">The link.</param>public override void AddLink(Link<T> link){ if (link == null) { throw new ArgumentNullException("link"); } if (link.Source != null && link.Target != null) { var foundLink = this.InternalLinks.FirstOrDefault(x => (IsSameNode(x.Source, link.Source) && IsSameNode(x.Target, link.Target)) || (IsSameNode(x.Target, link.Source) && IsSameNode(x.Source, link.Target))); if (foundLink == null) { link.Source = this.GetNode(link.Source.Entity.GetIdentifier()); link.Target = this.GetNode(link.Target.Entity.GetIdentifier()); base.AddLink(link); } }}/// <summary>/// Removes the item./// </summary>/// <param name="node">The node.</param>/// <returns>Removes the node and its connections.</returns>public override bool RemoveItem(Node<T> node){ this.InternalLinks.Remove(x => (IsSameNode(x.Source, node) && IsSameNode(x.Target, node))); return base.RemoveItem(node);}/// <summary>/// Determines whether [is same node] [the specified node1]./// </summary>/// <param name="node1">The node1.</param>/// <param name="node2">The node2.</param>/// <returns>True if the nodes have the same id.</returns>private static bool IsSameNode(Node<T> node1, Node<T> node2){ if (node1 == null) { return false; } if (node2 == null) { return false; } return node1.Entity.GetIdentifier() == node2.Entity.GetIdentifier();}