예제 #1
0
        public virtual void CommitLinks(BaseEntity entity)
        {
            if (Settings.IsVerbose)
                Console.WriteLine ("  Committing all links for '"  + entity.GetType().Name + "'.");

            var previousEntity = Reader.Read(entity.GetType(), entity.Id);

            FindAndFixDifferences (previousEntity, entity);
        }
예제 #2
0
        public virtual void FindAndFixDifferences(BaseEntity previousEntity, BaseEntity updatedEntity)
        {
            if (Settings.IsVerbose)
                Console.WriteLine ("    Finding and fixing all differences between previous and updated '" + updatedEntity.GetType().Name + "' entity.");

            foreach (var property in updatedEntity.GetType().GetProperties()) {
                if (Linker.IsLinkProperty (updatedEntity, property)) {
                    FindAndFixDifferences (previousEntity, updatedEntity, property);
                }
            }
        }
예제 #3
0
        public virtual void FindAndFixDifferences(BaseEntity previousEntity, BaseEntity updatedEntity, PropertyInfo property)
        {
            if (Settings.IsVerbose)
                Console.WriteLine ("      Finding and fixing all differences between previous and updated '" + updatedEntity.GetType().Name + "' entity on '" + property.Name + "' property.");

            var previousLinks = new BaseEntity[]{ };

            if (previousEntity != null)
                previousLinks = Linker.GetLinkedEntities (previousEntity, property);

            var updatedLinks = Linker.GetLinkedEntities (updatedEntity, property);

            var linksToAdd = IdentifyEntityLinksToAdd (previousLinks, updatedLinks);

            var linksToRemove = IdentifyEntityLinksToRemove (previousLinks, updatedLinks);

            if (Settings.IsVerbose) {
                Console.WriteLine ("      Links to add: " + linksToAdd.Length);
                Console.WriteLine ("      Links to remove: " + linksToRemove.Length);
            }

            if (linksToAdd.Length > 0)
                CommitNewReverseLinks (updatedEntity, property, linksToAdd);

            if (linksToRemove.Length > 0)
                RemoveOldReverseLinks (updatedEntity, property, linksToRemove);
        }
예제 #4
0
        public bool Exists(BaseEntity entity)
        {
            if (Settings.IsVerbose)
                Console.WriteLine ("Checking if entity exists: " + entity.GetType().Name);

            var entityType = entity.GetType ();

            var foundEntity = Reader.Read(entityType, entity.Id);

            var exists = foundEntity != null;

            if (Settings.IsVerbose)
                Console.WriteLine ("  Exists: " + exists);

            return exists;
        }
예제 #5
0
        public void Update(BaseEntity entity)
        {
            if (Checker.Exists (entity)) {
                if (Settings.IsVerbose)
                    Console.WriteLine ("Updating: " + entity.GetType ().Name);

                Linker.CommitLinks (entity);

                InternalUpdate (entity);
            } else// if (!Data.PendingSave.Contains(entity)) // TODO: Remove if not needed
                throw new EntityNotFoundException (entity);
        }
예제 #6
0
        public virtual void CommitNewReverseLinks(BaseEntity entity, PropertyInfo property, BaseEntity[] newLinkedEntities)
        {
            if (Settings.IsVerbose)
                Console.WriteLine ("    Committing new reverse links for '" + entity.GetType ().Name + "' entity on '" + property.Name + "'.");

            var otherPropertyName = Linker.GetOtherPropertyName (property);

            if (!String.IsNullOrEmpty (otherPropertyName)) {
                foreach (var newLinkedEntity in newLinkedEntities) {
                    if (!Saver.PendingSave.Contains (newLinkedEntity)
                        && !Updater.PendingUpdate.Contains(newLinkedEntity)) {
                        Linker.AddReturnLink (entity, property, newLinkedEntity, otherPropertyName);

                        Updater.DelayUpdate (newLinkedEntity);
                    }
                }
            }
        }
예제 #7
0
        public void Save(BaseEntity entity, bool commitLinks)
        {
            if (!Checker.Exists (entity)) {
                var entityType = entity.GetType ();

                if (Settings.IsVerbose)
                    Console.WriteLine ("Saving: " + entityType.Name);

                TypeManager.EnsureExists (entityType);

                // Commit links before saving, otherwise it will fail
                if (commitLinks)
                    Linker.CommitLinks (entity);

                InternalSave (entity);
            } else
                throw new EntityAlreadyExistsException (entity);
        }
예제 #8
0
        public virtual void UpdateLinkedEntities(BaseEntity entity, PropertyInfo property)
        {
            if (Settings.IsVerbose)
                Console.WriteLine ("Updating all entities linked to '"  + entity.GetType().Name + "' on '" + property.Name + "' property.");

            var linkedEntities = Linker.GetLinkedEntities (entity, property);

            foreach (var e in linkedEntities) {
                if (Checker.Exists(e))
                    Updater.Update (e);
            }
        }
예제 #9
0
        public virtual void UpdateLinkedEntities(BaseEntity entity)
        {
            if (Settings.IsVerbose)
                Console.WriteLine ("Updating all entities linked to '"  + entity.GetType().Name + "'.");

            foreach (var property in entity.GetType().GetProperties()) {
                if (Linker.IsLinkProperty (entity, property)) {
                    UpdateLinkedEntities (entity, property);
                }
            }
        }
예제 #10
0
        public virtual void SaveLinkedEntities(BaseEntity entity, PropertyInfo property)
        {
            if (Settings.IsVerbose)
                Console.WriteLine ("Saving all entities linked to '"  + entity.GetType().Name + "' on '" + property.Name + "' property.");

            var linkedEntities = Linker.GetLinkedEntities (entity, property);

            foreach (var e in linkedEntities) {
                if (!Checker.Exists (e)) {
                    Saver.Save(e, false); // Save without committing links otherwise it causes a loop
                }
            }
        }
 public UnsavedLinksException(BaseEntity entity)
     : base("Some of the entities linked to '" + entity.GetType().Name + " have not been saved. Use the Data.SaveLinkedEntities(entity) function.")
 {
 }
예제 #12
0
        public virtual void RemoveLinks(BaseEntity entity, PropertyInfo property, string otherPropertyName)
        {
            if (Settings.IsVerbose)
                Console.WriteLine ("    Removing all links for '"  + entity.GetType().Name + "' on '" + property.Name + "' property.");

            if (!String.IsNullOrEmpty (otherPropertyName)) {
                var linkedEntities = Linker.GetLinkedEntities (entity, property);

                foreach (var linkedEntity in linkedEntities) {
                    if (linkedEntity != null) {
                        Linker.RemoveReturnLink (entity, property, linkedEntity, otherPropertyName);

                        // Delay update until all references are fixed
                        Updater.DelayUpdate (linkedEntity);
                    }
                }
            }
        }
예제 #13
0
        public virtual void RemoveLinks(BaseEntity entity)
        {
            if (Settings.IsVerbose)
            {
                Console.WriteLine ("  Removing links between '" + entity.GetType().Name + "' and other entities");
            }

            foreach (var property in entity.GetType().GetProperties()) {
                var otherPropertyName = "";

                // TODO: Clean up if statement
                if ((Linker.PropertyHasLinkAttribute(property, out otherPropertyName)
                    || Linker.IsLinkProperty(entity, property))
                    && !String.IsNullOrEmpty(otherPropertyName))
                {
                    RemoveLinks (entity, property, otherPropertyName);
                }
            }
        }
 public EntityAlreadyExistsException(BaseEntity entity)
     : base("'" + entity.GetType ().Name + "' with the same ID already exists in the data store.")
 {
 }
예제 #15
0
        public void Remove(BaseEntity entity)
        {
            var ids = new List<string>(GetIds (entity.GetType().Name));

            if (!ids.Contains (entity.Id))
                ids.Remove (entity.Id);

            SetIds (entity.GetType (), ids.ToArray ());
        }
예제 #16
0
 public void Add(BaseEntity entity)
 {
     Add (entity.GetType(), entity.Id);
 }
 public EntityNotFoundException(BaseEntity entity)
     : base("'" + entity.GetType ().Name + "' entity not found in data store.")
 {
     Entity = entity;
 }
예제 #18
0
        public virtual void RemoveOldReverseLinks(BaseEntity entity, PropertyInfo property, BaseEntity[] oldLinkedEntities)
        {
            if (Settings.IsVerbose)
                Console.WriteLine ("    Removing obsolete reverse links for '" + entity.GetType ().Name + "' entity on '" + property.Name + "'.");

            var otherPropertyName = Linker.GetOtherPropertyName (property);

            if (!String.IsNullOrEmpty (otherPropertyName)) {
                foreach (var oldLinkedEntity in oldLinkedEntities) {
                    Linker.RemoveReturnLink (entity, property, oldLinkedEntity, otherPropertyName);

                    if (!Updater.PendingUpdate.Contains (oldLinkedEntity))
                        Updater.DelayUpdate (oldLinkedEntity);
                }
            }
        }