Exemplo n.º 1
0
        private bool CanDelete(IEnumerable <IEntity> entities, Conflicts conflicts)
        {
            conflicts = conflicts ?? new Conflicts();

            var numberOfConflicts = conflicts.Deletes.Count;

            foreach (var entity in entities)
            {
                var set = For(entity);

                if (!set.TryGetValue(entity.Id, out var existing))
                {
                    conflicts.Deletes.Add(new EntityKey(entity)
                    {
                        Version = 0
                    });
                }
                else if (existing.Version != entity.Version)
                {
                    conflicts.Deletes.Add(new EntityKey(existing));
                }
            }

            return(numberOfConflicts == conflicts.Deletes.Count);
        }
Exemplo n.º 2
0
        private bool CanUpdate(IEnumerable <IEntity> entities, Conflicts conflicts = null)
        {
            conflicts = conflicts ?? new Conflicts();

            var numberOfConflicts = conflicts.Updates.Count;

            foreach (var entity in entities)
            {
                var set = For(entity);

                if (!set.ContainsKey(entity.Id))
                {
                    conflicts.Updates.Add(new EntityKey(entity)
                    {
                        Version = 0
                    });
                }
                else if (set[entity.Id].Version != entity.Version)
                {
                    conflicts.Updates.Add(new EntityKey(set[entity.Id]));
                }
            }

            return(numberOfConflicts == conflicts.Updates.Count);
        }
Exemplo n.º 3
0
        internal void Update(params IEntity[] entities)
        {
            var conflicts = new Conflicts();

            if (!CanUpdate(entities, conflicts))
            {
                throw new OptimisticConcurrencyException(conflicts);
            }

            DoUpsert(entities);
        }
Exemplo n.º 4
0
        public void Delete(params IEntity[] entities)
        {
            var conflicts = new Conflicts();

            if (!CanDelete(entities, conflicts))
            {
                throw new OptimisticConcurrencyException(conflicts);
            }

            DoDelete(entities);
        }
Exemplo n.º 5
0
        private bool CanInsert(IEnumerable <IEntity> entities, Conflicts conflicts = null)
        {
            conflicts = conflicts ?? new Conflicts();

            var numberOfConflicts = conflicts.Inserts.Count;

            foreach (var entity in entities)
            {
                var set = For(entity);

                if (set.TryGetValue(entity.Id, out var existing))
                {
                    conflicts.Inserts.Add(new EntityKey(existing));
                }
            }

            return(numberOfConflicts == conflicts.Inserts.Count);
        }
Exemplo n.º 6
0
        public void DoExecute(Batch batch)
        {
            var missingTypes = batch.Types().Except(_entities.Keys).ToArray();

            if (missingTypes.Any())
            {
                throw new MissingTypesException(missingTypes);
            }

            var conflicts = new Conflicts();

            if (!CanExecute(batch, conflicts))
            {
                throw new OptimisticConcurrencyException(conflicts);
            }

            DoUpsert(batch.Updates);
            DoUpsert(batch.Inserts);
            DoDelete(batch.Deletes);
        }
Exemplo n.º 7
0
 private bool CanExecute(Batch batch, Conflicts conflicts)
 {
     return(CanDelete(batch.Deletes, conflicts) &&
            CanInsert(batch.Inserts, conflicts) &&
            CanUpdate(batch.Updates, conflicts));
 }
Exemplo n.º 8
0
 public OptimisticConcurrencyException(Conflicts conflicts)
 {
     Conflicts = conflicts;
 }