Exemplo n.º 1
0
        public void SaveChanges()
        {
            try
            {
                IEnumerable <EntityChangeDescriptor> temp = ChangeTracker.GetChangeLog();

                List <Tuple <IOgmEntity, IEnumerable <string> > > creatingNodes = temp
                                                                                  .Where(p => p is EntityChangeNodeCreation)
                                                                                  .Select(p => new Tuple <IOgmEntity, IEnumerable <string> >(
                                                                                              p.Entity,
                                                                                              TypesManager.KnownTypes.ContainsKey(p.Entity.GetType()) ? TypesManager.KnownTypes[p.Entity.GetType()].IgnoredProperties.Select(q => q.Name) : null
                                                                                              )).ToList();

                List <IOgmEntity> deletingNodes = temp
                                                  .Where(p => p is EntityChangeNodeDeletion)
                                                  .Select(p => p.Entity).ToList();

                List <Tuple <IOgmEntity, IEnumerable <string> > > updatingNodes = temp
                                                                                  .Where(p => p is EntityChangeNodeUpdate)
                                                                                  .GroupBy(p => p.Entity)
                                                                                  .Select(p => new Tuple <IOgmEntity, IEnumerable <string> >(
                                                                                              p.Key,
                                                                                              p.Key.GetType().GetProperties().Select(q => q.Name).Except(p.Select(q => ((EntityChangeNodeUpdate)q).Property.Name))
                                                                                              .Union(
                                                                                                  TypesManager.KnownTypes.ContainsKey(p.Key.GetType()) ? TypesManager.KnownTypes[p.Key.GetType()].IgnoredProperties.Select(q => q.Name) : new string[0]
                                                                                                  )
                                                                                              )).ToList();

                List <Tuple <IOgmConnection, IEnumerable <string> > > creatingRels = temp
                                                                                     .Where(p => p is EntityChangeRelCreation)
                                                                                     .Select(p => new Tuple <IOgmConnection, IEnumerable <string> >(
                                                                                                 p.Entity as IOgmConnection,
                                                                                                 TypesManager.KnownTypes.ContainsKey(p.Entity.GetType()) ? TypesManager.KnownTypes[p.Entity.GetType()].IgnoredProperties.Select(q => q.Name) : null
                                                                                                 )).ToList();

                List <IOgmConnection> deletingRels = temp
                                                     .Where(p => p is EntityChangeRelDeletion)
                                                     .Select(p => p.Entity as IOgmConnection).ToList();

                List <Tuple <IOgmConnection, IEnumerable <string> > > updatingRels = temp
                                                                                     .Where(p => p is EntityChangeRelUpdate)
                                                                                     .GroupBy(p => p.Entity)
                                                                                     .Select(p => new Tuple <IOgmConnection, IEnumerable <string> >(
                                                                                                 p.Key as IOgmConnection,
                                                                                                 p.Key.GetType().GetProperties().Select(q => q.Name).Except(p.Select(q => ((EntityChangeRelUpdate)q).Property.Name))
                                                                                                 .Union(
                                                                                                     TypesManager.KnownTypes.ContainsKey(p.Key.GetType()) ? TypesManager.KnownTypes[p.Key.GetType()].IgnoredProperties.Select(q => q.Name) : new string[0]
                                                                                                     )
                                                                                                 )).ToList();

                List <Tuple <IOgmConnection, IEnumerable <string> > > mergingRels = temp
                                                                                    .Where(p => p is EntityChangeConnectionMerge)
                                                                                    .Select(p => new Tuple <IOgmConnection, IEnumerable <string> >(
                                                                                                p.Entity as IOgmConnection,
                                                                                                TypesManager.KnownTypes.ContainsKey(p.Entity.GetType()) ? TypesManager.KnownTypes[p.Entity.GetType()].IgnoredProperties.Select(q => q.Name) : null
                                                                                                )).ToList();

                List <IOgmEntity> createdNodes = EntityManager.CreateNodes(Runner, creatingNodes)?.ToList();
                EntityManager.UpdateNodes(Runner, updatingNodes);
                EntityManager.DeleteNodes(Runner, deletingNodes);

                for (int i = 0; i < (createdNodes?.Count ?? 0); i++)
                {
                    creatingNodes[i].Item1.EntityId = createdNodes[i].EntityId;
                }

                List <IOgmConnection> createdRels = EntityManager.CreateRels(Runner, creatingRels)?.ToList();

                List <IOgmConnection> mergedRels = EntityManager.MergeConnections(Runner, mergingRels)?.ToList();

                EntityManager.UpdateRels(Runner, updatingRels);
                EntityManager.DeleteRels(Runner, deletingRels);

                for (int i = 0; i < (createdRels?.Count ?? 0); i++)
                {
                    creatingRels[i].Item1.EntityId = createdRels[i].EntityId;
                }
                for (int i = 0; i < (mergedRels?.Count ?? 0); i++)
                {
                    mergingRels[i].Item1.EntityId = mergedRels[i].EntityId;
                }

                ChangeTracker.Clear();
            }
            catch (Exception e)
            {
                throw e;
            }
        }