Beispiel #1
0
 public void AddParentChildRelationship(ParentChildRelationship parentChildRelationship)
 {
     if (RelationshipCreatesCycle(parentChildRelationship))
     {
         throw new ArgumentException($"Adding {parentChildRelationship.Parent.Id}->{parentChildRelationship.Child.Id} relationship would create a circular reference.");
     }
     ParentChildRelationships.Add(parentChildRelationship); // store in DB
 }
Beispiel #2
0
        private bool AnyDescendantEquals(Entity condition, ParentChildRelationship childToTest, HashSet <int> visitedIds = null)
        {
            var visited = visitedIds ?? new HashSet <int>();

            if (childToTest.Child.Id == condition.Id)
            {
                return(true);
            }
            visited.Add(childToTest.Child.Id);
            var childrenOfChildToTest = ParentChildRelationships.Where(x => x.Parent.Id == childToTest.Child.Id);

            return(childrenOfChildToTest.Any(r => !visited.Contains(r.Child.Id) && AnyDescendantEquals(condition, r, visited)));
        }
Beispiel #3
0
 private bool RelationshipCreatesCycle(ParentChildRelationship parentChildRelationship)
 {
     return(AnyDescendantEquals(parentChildRelationship.Parent, parentChildRelationship));
 }