private static bool WasItemRemoved(IChangeSet <IRoutableViewModel> changeSet, IRoutableViewModel item)
 {
     return(changeSet
            .Any(
                change =>
     {
         return
         change.Reason == ListChangeReason.Clear ||
         (NavigationStackRemovalOperations.Contains(change.Reason) && change.Item.Current == item);
     }));
 }
Exemple #2
0
        /// <summary>
        /// Persists changes to the data store
        /// </summary>
        protected virtual void SaveChanges(TRepository repository, IChangeSet changeSet, IResultSet resultSet)
        {
            // don't save in case of errors
            if (changeSet.Any(entity => entity.Errors.Any(error => error.IsError)))
            {
                return;
            }

            var optimisticConcurrencyExceptionEntities = new List <Entity>();
            var updateException = new List <Entity>();
            var allSaved        = false;

            while (!allSaved)
            {
                try
                {
                    repository.SaveChanges();
                    allSaved = true;
                }
                catch (UpdateException ex)
                {
                    var entities = ex.Entities;

                    // refresh changed entities from store
                    if (entities.Any(x => x.ChangeTracker.State != ObjectState.Added))
                    {
                        repository.Refresh(RefreshMode.StoreWins, entities.Where(x => x.ChangeTracker.State != ObjectState.Added));
                    }

                    // remove new entities from repository
                    if (entities.Any(x => x.ChangeTracker.State == ObjectState.Added))
                    {
                        foreach (var entity in entities.Where(x => x.ChangeTracker.State == ObjectState.Added))
                        {
                            DetachEntity(repository, entity);
                        }
                    }

                    AddExceptionMessageAsErrorEntry(ex, entities);

                    // collect faulted entities
                    if (ex is OptimisticConcurrencyException)
                    {
                        optimisticConcurrencyExceptionEntities.AddRange(entities);
                    }
                    else
                    {
                        updateException.AddRange(entities);
                    }

                    if (ThrowOnFirstUpdateException)
                    {
                        break;
                    }
                }
            }

            if (updateException.Any())
            {
                var message = "Update, insert, or delete statement failed for one or more entities. Transaction was rolled back.";
                throw CreateUpdateFaultException(message, updateException.Concat(optimisticConcurrencyExceptionEntities));
            }

            if (optimisticConcurrencyExceptionEntities.Any())
            {
                var message = "Update, insert, or delete statement failed for one or more entities. Transaction was rolled back.";
                throw CreateOptimisticConcurrencyFaultException(message, optimisticConcurrencyExceptionEntities);
            }
        }