private void RemoveNotifyPropertyChangedHandlers()
 {
     foreach (var node in EntityRelationGraph.OfType <INotifyPropertyChanged>())
     {
         node.PropertyChanged -= node_PropertyChanged;
     }
 }
 protected void EntityRelationGraphReset()
 {
     if (EntityRelationGraphResetting != null)
     {
         EntityRelationGraphResetting(this, new EventArgs());
     }
     EntityRelationGraph = null;
     if (EntityRelationGraphResetted != null)
     {
         EntityRelationGraphResetted(this, new EventArgs());
     }
 }
Exemple #3
0
        /// <summary>
        /// (Re-)builds the associations between the nodes of the graph.
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="graph"></param>
        private static void BuildEntityGraph(Dictionary <TEntity, TEntity> nodes, EntityRelationGraph <TEntity> graph)
        {
            foreach (var n in graph.Nodes)
            {
                var newEntity = nodes[n.Node];
                foreach (var association in n.SingleEdges.Keys)
                {
                    var oldAssociationEntity = n.SingleEdges[association];
                    var newAssociationEntity = nodes[oldAssociationEntity];
                    association.SetValue(newEntity, newAssociationEntity, null);
                }
                foreach (var association in n.ListEdges.Keys)
                {
                    IEnumerable assocList     = (IEnumerable)association.GetValue(newEntity, null);
                    Type        assocListType = assocList.GetType();
                    var         addMethod     = assocListType.GetMethod("Add");

                    foreach (var oldAssociationEntity in n.ListEdges[association])
                    {
                        var newAssociationEntity = nodes[oldAssociationEntity];
                        addMethod.Invoke(assocList, new object[] { newAssociationEntity });
                    }
                }
#if SILVERLIGHT
                // The code below is to fix an error in RIA where relationship span is not performed
                // for newly created entities.
                // This means that for an association that is not included in the entity graph,
                // the 'newEntity' entity would include the foreing key to that entity, but since no relationship span
                // takes place, the corresponding association is not bound to that entity.
                // Below we set these association properties ourselves. We detect newly created entities by
                // the heuristic that they don't have an origional state. We only set the association if the
                // corresponding entity is detached from the context, otherwise the 'newEntity' entity would be
                // added to the context as a side effect.
                foreach (PropertyInfo association in GetAssociations(newEntity))
                {
                    if (association.PropertyType.IsSubclassOf(typeof(TEntity)))
                    {
                        TEntity e = (TEntity)association.GetValue(n.Node, null);
                        if (e != null && e is System.ServiceModel.DomainServices.Client.Entity)
                        {
                            if ((e as System.ServiceModel.DomainServices.Client.Entity).GetOriginal() == null)
                            {
                                if ((e as System.ServiceModel.DomainServices.Client.Entity).EntityState == System.ServiceModel.DomainServices.Client.EntityState.Detached)
                                {
                                    association.SetValue(newEntity, nodes.ContainsKey(e) ? nodes[e] : e, null);
                                }
                            }
                        }
                    }
                }
#endif
            }
        }
        /// <summary>
        /// Returns the entity graph as defined by associations that are marked with the 'EntityGraphAttribute' attribute.
        /// The resulting graph consists of a list of GraphNodes. Each GraphNode has an element 'Node' of type 'T',
        /// which represents the actual node, a set, SingleEdges, which correspond to EntityRefs, and a
        /// a set, ListEdges, which correspond to EntityCollections.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="graph"></param>
        private void BuildEntityGraph(TEntity entity, EntityRelationGraph <TEntity> graph)
        {
            if (graph.Nodes.Any(n => n.Node == entity))
            {
                return;
            }
            EntityRelation <TEntity> node = new EntityRelation <TEntity>()
            {
                Node = entity
            };

            graph.Nodes.Add(node);

            foreach (PropertyInfo edge in GraphShape.OutEdges(entity))
            {
                if (typeof(IEnumerable).IsAssignableFrom(edge.PropertyType))
                {
                    var assocList = GraphShape.GetNodes(entity, edge);
                    if (assocList == null)
                    {
                        continue;
                    }
                    node.ListEdges.Add(edge, new List <TEntity>());
                    foreach (TEntity e in assocList)
                    {
                        if (e != null)
                        {
                            node.ListEdges[edge].Add(e);
                            BuildEntityGraph(e, graph);
                        }
                    }
                }
                else
                {
                    TEntity e = (TEntity)GraphShape.GetNode(entity, edge);
                    if (e != null)
                    {
                        node.SingleEdges.Add(edge, e);
                        BuildEntityGraph(e, graph);
                    }
                }
            }
        }
Exemple #5
0
 public IEnumerator <TEntity> GetEnumerator()
 {
     return(EntityRelationGraph.GetEnumerator());
 }
Exemple #6
0
 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
 {
     return(EntityRelationGraph.GetEnumerator());
 }