private void OnConnectNodesCompleted(object sender, ConnectNodesCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                List <INodeProxy> nodes = new List <INodeProxy>();

                ConnectedNodesResult connectResult = e.Result;

                foreach (SoapNode soapNode in connectResult.Nodes.Values)
                {
                    if (_cachedNodes.ContainsKey(soapNode.Id))
                    {
                        _cachedNodes.Remove(soapNode.Id);
                    }

                    NodeProxy node = new NodeProxy(soapNode);
                    _cachedNodes.Add(soapNode.Id, node);
                    nodes.Add(node);
                }

                foreach (INodeProxy nodeProxy in nodes)
                {
                    foreach (IDescriptorProxy descriptorProxy in nodeProxy.Descriptors)
                    {
                        CompleteRelationship(descriptorProxy.Relationship);
                    }
                }

                ConnectedNodesEventArgs connectedNodesEventArgs = new ConnectedNodesEventArgs();
                connectedNodesEventArgs.Nodes        = nodes.ToArray();
                connectedNodesEventArgs.Relationship = new RelationshipProxy(e.Result.Relationship);

                CompleteRelationship(connectedNodesEventArgs.Relationship);

                //When a node is connected via a MapContainerRelationship the UserState will be the location of the new node
                //on the map, it can't be stored in the db until the relationship exists since it's the contectual relationship
                //that determines where it is located in it's view in this map (it may be elsewhere in transclusions)
                if (e.UserState != null)
                {
                    INodeProxy nodeProxy = connectedNodesEventArgs.Nodes[1];
                    Point      location  = (Point)e.UserState;
                    if (location != null)
                    {
                        TypeManager          typeManager    = IoC.IoCContainer.GetInjectionInstance().GetInstance <TypeManager>();
                        IDescriptorTypeProxy descriptorType = null;
                        if (e.Result.Relationship.RelationshipType.Name == "TransclusionRelationship")
                        {
                            descriptorType = typeManager.GetDescriptorType("TransclusionMap");
                        }
                        else
                        {
                            descriptorType = typeManager.GetDescriptorType("From");
                        }

                        MetadataContext xPositionKey = new MetadataContext()
                        {
                            NodeUid           = nodeProxy.Id,
                            RelationshipUid   = e.Result.Relationship.Id,
                            DescriptorTypeUid = descriptorType.Id,
                            MetadataName      = "XPosition"
                        };
                        MetadataContext yPositionKey = new MetadataContext()
                        {
                            NodeUid           = nodeProxy.Id,
                            RelationshipUid   = e.Result.Relationship.Id,
                            DescriptorTypeUid = descriptorType.Id,
                            MetadataName      = "YPosition"
                        };

                        if (nodeProxy.Metadata != null && nodeProxy.GetNodeMetadata(xPositionKey) != null)
                        {
                            nodeProxy.GetNodeMetadata(xPositionKey).MetadataValue = location.X.ToString();
                        }
                        else
                        {
                            MetadataTypeProxy metaDataTypeProxy = typeManager.GetMetadataType("double") as MetadataTypeProxy;
                            if (metaDataTypeProxy != null)
                            {
                                SoapMetadata soapMetadata = new SoapMetadata();
                                soapMetadata.MetadataName  = "XPosition";
                                soapMetadata.MetadataType  = metaDataTypeProxy.BaseSoapNodeType;
                                soapMetadata.MetadataValue = location.X.ToString();
                                nodeProxy.Metadata.Add(xPositionKey, soapMetadata);
                            }
                        }

                        if (nodeProxy.Metadata != null && nodeProxy.GetNodeMetadata(yPositionKey) != null)
                        {
                            nodeProxy.GetNodeMetadata(yPositionKey).MetadataValue = location.Y.ToString();
                        }
                        else
                        {
                            MetadataTypeProxy metaDataTypeProxy = typeManager.GetMetadataType("double") as MetadataTypeProxy;
                            if (metaDataTypeProxy != null)
                            {
                                SoapMetadata soapMetadata = new SoapMetadata();
                                soapMetadata.MetadataName  = "YPosition";
                                soapMetadata.MetadataType  = metaDataTypeProxy.BaseSoapNodeType;
                                soapMetadata.MetadataValue = location.Y.ToString();
                                nodeProxy.Metadata.Add(yPositionKey, soapMetadata);
                            }
                        }
                    }
                }

                if (ConnectNodesCompleted != null)
                {
                    ConnectNodesCompleted.Invoke(this, connectedNodesEventArgs);
                }
            }
        }
        private void OnConnectNodesCompleted(object sender, ConnectNodesCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                List<INodeProxy> nodes = new List<INodeProxy>();

                ConnectedNodesResult connectResult = e.Result;

                foreach (SoapNode soapNode in connectResult.Nodes.Values)
                {
                    if (_cachedNodes.ContainsKey(soapNode.Id))
                    {
                        _cachedNodes.Remove(soapNode.Id);
                    }

                    NodeProxy node = new NodeProxy(soapNode);
                    _cachedNodes.Add(soapNode.Id, node);
                    nodes.Add(node);
                }

                foreach (INodeProxy nodeProxy in nodes)
                {
                    foreach (IDescriptorProxy descriptorProxy in nodeProxy.Descriptors)
                    {
                        CompleteRelationship(descriptorProxy.Relationship);
                    }
                }

                ConnectedNodesEventArgs connectedNodesEventArgs = new ConnectedNodesEventArgs();
                connectedNodesEventArgs.Nodes = nodes.ToArray();
                connectedNodesEventArgs.Relationship = new RelationshipProxy(e.Result.Relationship);

                CompleteRelationship(connectedNodesEventArgs.Relationship);

                //When a node is connected via a MapContainerRelationship the UserState will be the location of the new node
                //on the map, it can't be stored in the db until the relationship exists since it's the contectual relationship
                //that determines where it is located in it's view in this map (it may be elsewhere in transclusions)
                if (e.UserState != null)
                {
                    INodeProxy nodeProxy = connectedNodesEventArgs.Nodes[1];
                    Point location = (Point)e.UserState;
                    if (location != null)
                    {
                        TypeManager typeManager = IoC.IoCContainer.GetInjectionInstance().GetInstance<TypeManager>();
                        IDescriptorTypeProxy descriptorType = null;
                        if (e.Result.Relationship.RelationshipType.Name == "TransclusionRelationship")
                        {
                            descriptorType = typeManager.GetDescriptorType("TransclusionMap");
                        }
                        else
                        {
                            descriptorType = typeManager.GetDescriptorType("From");
                        }
                        
                        MetadataContext xPositionKey = new MetadataContext()
                        {
                            NodeUid = nodeProxy.Id,
                            RelationshipUid = e.Result.Relationship.Id,
                            DescriptorTypeUid = descriptorType.Id,
                            MetadataName = "XPosition"
                        };
                        MetadataContext yPositionKey = new MetadataContext()
                        {
                            NodeUid = nodeProxy.Id,
                            RelationshipUid = e.Result.Relationship.Id,
                            DescriptorTypeUid = descriptorType.Id,
                            MetadataName = "YPosition"
                        };

                        if (nodeProxy.Metadata != null && nodeProxy.GetNodeMetadata(xPositionKey) != null)
                        {
                            nodeProxy.GetNodeMetadata(xPositionKey).MetadataValue = location.X.ToString();
                        }
                        else
                        {
                            MetadataTypeProxy metaDataTypeProxy = typeManager.GetMetadataType("double") as MetadataTypeProxy;
                            if (metaDataTypeProxy != null)
                            {
                                SoapMetadata soapMetadata = new SoapMetadata();
                                soapMetadata.MetadataName = "XPosition";
                                soapMetadata.MetadataType = metaDataTypeProxy.BaseSoapNodeType;
                                soapMetadata.MetadataValue = location.X.ToString();
                                nodeProxy.Metadata.Add(xPositionKey, soapMetadata);
                            }
                        }

                        if (nodeProxy.Metadata != null && nodeProxy.GetNodeMetadata(yPositionKey) != null)
                        {
                            nodeProxy.GetNodeMetadata(yPositionKey).MetadataValue = location.Y.ToString();
                        }
                        else
                        {
                            MetadataTypeProxy metaDataTypeProxy = typeManager.GetMetadataType("double") as MetadataTypeProxy;
                            if (metaDataTypeProxy != null)
                            {
                                SoapMetadata soapMetadata = new SoapMetadata();
                                soapMetadata.MetadataName = "YPosition";
                                soapMetadata.MetadataType = metaDataTypeProxy.BaseSoapNodeType;
                                soapMetadata.MetadataValue = location.Y.ToString();
                                nodeProxy.Metadata.Add(yPositionKey, soapMetadata);
                            }
                        }
                    }
                }

                if (ConnectNodesCompleted != null)
                {
                    ConnectNodesCompleted.Invoke(this, connectedNodesEventArgs);
                }
            }
        }