Beispiel #1
0
 public void HasGetSet()
 {
     var value = new Place();
     var entity = new PlaceNode { Offspring = value };
     entity.ShouldNotBeNull();
     entity.Offspring.ShouldEqual(value);
 }
Beispiel #2
0
 public void HasGetSet()
 {
     const int value = 5;
     var entity = new PlaceNode { OffspringId = value };
     entity.ShouldNotBeNull();
     entity.OffspringId.ShouldEqual(value);
 }
Beispiel #3
0
 public void HasGetSet()
 {
     var value = new Place();
     var entity = new PlaceNode { Ancestor = value };
     entity.ShouldNotBeNull();
     entity.Ancestor.ShouldEqual(value);
 }
Beispiel #4
0
 public void HasGetSet()
 {
     const int value = 3;
     var entity = new PlaceNode { AncestorId = value };
     entity.ShouldNotBeNull();
     entity.AncestorId.ShouldEqual(value);
 }
Beispiel #5
0
            public void HasGetSet()
            {
                var value  = new Place();
                var entity = new PlaceNode {
                    Offspring = value
                };

                entity.ShouldNotBeNull();
                entity.Offspring.ShouldEqual(value);
            }
Beispiel #6
0
            public void HasGetSet()
            {
                const int value  = 5;
                var       entity = new PlaceNode {
                    OffspringId = value
                };

                entity.ShouldNotBeNull();
                entity.OffspringId.ShouldEqual(value);
            }
Beispiel #7
0
            public void HasGetSet()
            {
                var value  = new Place();
                var entity = new PlaceNode {
                    Ancestor = value
                };

                entity.ShouldNotBeNull();
                entity.Ancestor.ShouldEqual(value);
            }
Beispiel #8
0
            public void HasGetSet()
            {
                const int value  = 3;
                var       entity = new PlaceNode {
                    AncestorId = value
                };

                entity.ShouldNotBeNull();
                entity.AncestorId.ShouldEqual(value);
            }
Beispiel #9
0
        public void Handle(EnsurePlaceHierarchy command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // when place is transient, its parent or any ancestors may also be transient
            var isTransient  = command.Place.RevisionId == default(int);
            var oldAncestors = new Collection <PlaceNode>(command.Place.Ancestors.ToArray());
            var newAncestors = new Collection <PlaceNode>();
            var addAncestors = new Collection <PlaceNode>();
            var separation   = 1;
            var parent       = command.Place.Parent;

            while (parent != null)
            {
                // create the node to reference its foreign keys
                var entity = new PlaceNode
                {
                    Ancestor   = parent,
                    Offspring  = command.Place,
                    Separation = separation,
                };
                if (!isTransient)
                {
                    entity.AncestorId  = parent.RevisionId;
                    entity.OffspringId = command.Place.RevisionId;
                }
                newAncestors.Add(entity); // hang onto the ancestor node

                // does this ancestor already exist?
                var ancestor = isTransient
                    ? command.Place.Ancestors.FirstOrDefault(x => ReferenceEquals(x.Ancestor, parent))
                    : command.Place.Ancestors.FirstOrDefault(x => x.AncestorId == parent.RevisionId);
                if (ancestor != null) // yes this ancestor is already present
                {
                    ancestor.Separation = separation;
                }
                else
                {
                    addAncestors.Add(entity);
                    _entities.Create(entity);
                }
                ++separation;
                parent = parent.Parent;
            }

            // any ancestors to delete?
            var ancestorsToDelete = isTransient
                ? oldAncestors.Where(x => !newAncestors.Select(y => y.Ancestor).Contains(x.Ancestor)).ToArray()
                : oldAncestors.Where(x => !newAncestors.Select(y => y.AncestorId).Contains(x.AncestorId)).ToArray();

            if (ancestorsToDelete.Any())
            {
                foreach (var ancestorToDelete in ancestorsToDelete)
                {
                    _entities.Purge(ancestorToDelete);
                }
            }
        }