Exemplo n.º 1
0
        /// <inheritdoc/>
        /// <exception cref="ValidationException">Validations errors.</exception>
        protected override void ValidateState()
        {
            using (var ea = new ExceptionAggregator()) {
                ea.Execute(base.ValidateState);

                if (PrimaryKey == null)
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExUndefinedPrimaryKey, Path);
                    });
                }

                var pkTypes = PrimaryKey.KeyColumns.Select(c => c.Value.Type);
                var fkTypes = ForeignKeyColumns.Select(c => c.Value.Type);
                if (pkTypes.Count() != pkTypes.Zip(fkTypes).Where(p => p.First.Type == p.Second.Type.StripNullable()).Count())
                {
                    ea.Execute(() => {
                        throw new ValidationException(
                            Strings.ExInvalidForeignKeyStructure, Path);
                    });
                }

                ea.Complete();
            }
        }
Exemplo n.º 2
0
 /// <inheritdoc/>
 public void Validate()
 {
     using (var ea = new ExceptionAggregator()) {
         foreach (var node in list)
         {
             ea.Execute(x => x.Validate(), node);
         }
         ea.Complete();
     }
 }
 public void ProcessFinalizers()
 {
     using (var ea = new ExceptionAggregator()) {
         // Backward order
         for (int i = finalizers.Count - 1; i >= 0; i--)
         {
             ea.Execute(finalizers[i]);
         }
         ea.Complete();
     }
 }
 /// <inheritdoc/>
 /// <exception cref="ValidationException">Invalid <see cref="Direction"/> value
 /// (<see cref="Core.Direction.None"/>).</exception>
 protected override void ValidateState()
 {
     using (var ea = new ExceptionAggregator()) {
         ea.Execute(base.ValidateState);
         if (direction == Direction.None)
         {
             ea.Execute(() => {
                 throw new ValidationException(Strings.ExInvalidDirectionValue, Path);
             });
         }
         ea.Complete();
     }
 }
        /// <inheritdoc/>
        /// <exception cref="ValidationException">Validations errors.</exception>
        protected override void ValidateState()
        {
            using (var ea = new ExceptionAggregator()) {
                ea.Execute(base.ValidateState);

                if (PrimaryKey == null)
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExUndefinedPrimaryKey, Path);
                    });
                    ea.Complete();
                    return;
                }

                var pkColumns = PrimaryKey.KeyColumns;
                var fkColumns = ForeignKeyColumns;

                if (pkColumns.Count != pkColumns
                    .Zip(fkColumns, (pkColumn, fkColumn) => new Pair <KeyColumnRef, ForeignKeyColumnRef>(pkColumn, fkColumn))
                    .Count(p => CompareKeyColumns(p.First, p.Second)))
                {
                    ea.Execute(() => {
                        throw new ValidationException(
                            Strings.ExInvalidForeignKeyStructure, Path);
                    });
                }

                // var pkTypes = PrimaryKey.KeyColumns.Select(c => c.Value.Type);
                // var fkTypes = ForeignKeyColumns.Select(c => c.Value.Type);
                // if (pkTypes.Count()!=pkTypes.Zip(fkTypes).Where(p => p.First==p.Second).Count())
                //  ea.Execute(() => {
                //    throw new ValidationException(
                //      Strings.ExInvalidForeignKeyStructure, Path);
                //  });

                ea.Complete();
            }
        }
Exemplo n.º 6
0
 /// <inheritdoc/>
 /// <exception cref="ValidationException">Validation error.</exception>
 protected override void ValidateState()
 {
     using (var ea = new ExceptionAggregator()) {
         ea.Execute(base.ValidateState);
         if (Type == null)
         {
             ea.Execute(() => {
                 throw new ValidationException(
                     string.Format(Strings.ExUndefinedTypeOfColumnX, Name),
                     Path);
             });
         }
         ea.Complete();
     }
 }
        /// <exception cref="ValidationException"></exception>
        /// <inheritdoc/>
        protected override void ValidateState()
        {
            using (var ea = new ExceptionAggregator()) {
                ea.Execute(base.ValidateState);
                base.ValidateState();

                var tableColumns = Parent.Columns;
                var columns      = Columns.Select(keyRef => keyRef.Value).ToList();

                if (columns.Count == 0)
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExEmptyColumnsCollection, Path);
                    });
                }

                ea.Complete();
            }
        }
        /// <inheritdoc/>
        /// <exception cref="ValidationException">Validation error.</exception>
        protected override void ValidateState()
        {
            using (var ea = new ExceptionAggregator()) {
                ea.Execute(base.ValidateState);
                base.ValidateState();

                var tableColumns = Parent.Columns;
                var keys         = KeyColumns.Select(keyRef => keyRef.Value).ToList();
                var values       = ValueColumns.Select(valueRef => valueRef.Value).ToList();
                var all          = keys.Concat(values).ToList();

                if (keys.Count == 0)
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExEmptyKeyColumnsCollection, Path);
                    });
                }
                if (keys.Where(ci => ci.Type.IsNullable).Count() > 0)
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExPrimaryKeyColumnCanNotBeNullable, Path);
                    });
                }

                if (all.Count != tableColumns.Count)
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExInvalidPrimaryKeyStructure, Path);
                    });
                }
                if (all.Zip(tableColumns).Where(p => p.First != p.Second).Any())
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExInvalidPrimaryKeyStructure, Path);
                    });
                }

                ea.Complete();
            }
        }
 /// <inheritdoc/>
 /// <exception cref="ValidationException"><c>ValidationException</c>.</exception>
 protected override void ValidateState()
 {
     using (var ea = new ExceptionAggregator()) {
         ea.Execute(base.ValidateState);
         if (Increment <= 0)
         {
             ea.Execute(() => {
                 throw new ValidationException(
                     string.Format(Strings.ExInvalideIncrementValue),
                     Path);
             });
         }
         if (Type == null)
         {
             ea.Execute(() => {
                 throw new ValidationException(
                     string.Format(string.Format(Strings.ExUndefinedTypeOfSequenceX, Name)),
                     Path);
             });
         }
         ea.Complete();
     }
 }
Exemplo n.º 10
0
        /// <inheritdoc/>
        /// <exception cref="ValidationException">Empty secondary key columns collection.</exception>
        protected override void ValidateState()
        {
            using (var ea = new ExceptionAggregator()) {
                ea.Execute(base.ValidateState);

                // Secondary key columns: empty set, duplicates
                var keyColumns = KeyColumns.Select(valueRef => valueRef.Value).ToList();
                if (keyColumns.Count == 0)
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExEmptyKeyColumnsCollection, Path);
                    });
                }
                foreach (var group in keyColumns
                         .GroupBy(keyColumn => keyColumn)
                         .Where(group => group.Count() > 1))
                {
                    ea.Execute((_column) => {
                        throw new ValidationException(
                            string.Format(Strings.ExMoreThenOneKeyColumnReferenceToColumnX, _column.Name),
                            Path);
                    }, group.Key);
                }

                // Primary key columns
                if (PrimaryKeyColumns.Count != Parent.PrimaryIndex.KeyColumns.Count)
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExInvalidPrimaryKeyColumnsCollection, Path);
                    });
                }
                for (int i = 0; i < PrimaryKeyColumns.Count; i++)
                {
                    var ref1 = PrimaryKeyColumns[i];
                    var ref2 = Parent.PrimaryIndex.KeyColumns[i];
                    if (ref1.Value != ref2.Value || ref1.Direction != ref2.Direction)
                    {
                        ea.Execute(() => {
                            throw new ValidationException(Strings.ExInvalidPrimaryKeyColumnsCollection, Path);
                        });
                    }
                }

                // Included columns
                var fullKeySet = EnumerableExtensions.ToHashSet(KeyColumns
                                                                .Select(cr => cr.Value)
                                                                .Concat(PrimaryKeyColumns.Select(cr => cr.Value)));

                foreach (var columnRef in IncludedColumns)
                {
                    if (fullKeySet.Contains(columnRef.Value))
                    {
                        ea.Execute(() => {
                            throw new ValidationException(Strings.ExInvalidIncludedColumnsCollection, Path);
                        });
                    }
                }

                foreach (var group in IncludedColumns
                         .GroupBy(keyColumn => keyColumn)
                         .Where(group => group.Count() > 1))
                {
                    ea.Execute((_column) => {
                        throw new ValidationException(
                            string.Format(Strings.ExMoreThenOneIncludedColumnReferenceToColumnX, _column.Name),
                            Path);
                    }, group.Key);
                }

                ea.Complete();
            }
        }
Exemplo n.º 11
0
        public void Remove(IEnumerable <Entity> entities)
        {
            ArgumentValidator.EnsureArgumentNotNull(entities, "entities");
            bool isEmpty = true;

            foreach (var entity in entities)
            {
                isEmpty = false;
                entity.EnsureNotRemoved();
            }
            if (isEmpty)
            {
                return;
            }
            var processedEntities = new List <Entity>();
            var notifiedEntities  = new HashSet <Entity>();

            try {
                var operations = Session.Operations;
                using (var scope = operations.BeginRegistration(OperationType.System))
                    using (Context = new RemovalContext(this)) {
                        Session.EnforceChangeRegistrySizeLimit();
                        if (operations.CanRegisterOperation)
                        {
                            operations.RegisterOperation(
                                new EntitiesRemoveOperation(entities.Select(e => e.Key)));
                        }

                        Context.Enqueue(entities);

                        bool isOperationStarted = false;
                        while (!Context.QueueIsEmpty)
                        {
                            var entitiesForProcessing = Context.GatherEntitiesForProcessing();
                            foreach (var entity in entitiesForProcessing)
                            {
                                entity.SystemBeforeRemove();
                            }
                            if (!isOperationStarted)
                            {
                                isOperationStarted = true;
                                operations.NotifyOperationStarting();
                            }
                            ProcessItems(entitiesForProcessing);
                        }
                        if (!isOperationStarted)
                        {
                            operations.NotifyOperationStarting();
                        }

                        processedEntities = Context.GetProcessedEntities().ToList();
                        foreach (var entity in processedEntities)
                        {
                            entity.SystemRemove();
                            entity.State.PersistenceState = PersistenceState.Removed;
                        }
                        Context.ProcessFinalizers();
                        Session.EnforceChangeRegistrySizeLimit();

                        scope.Complete(); // Successful anyway

                        using (var ea = new ExceptionAggregator()) {
                            foreach (var entity in processedEntities)
                            {
                                ea.Execute(() => {
                                    notifiedEntities.Add(entity);
                                    entity.SystemRemoveCompleted(null);
                                });
                            }
                            ea.Complete();
                        }
                    }
            }
            catch (Exception e) {
                foreach (var entity in processedEntities)
                {
                    if (notifiedEntities.Contains(entity))
                    {
                        continue;
                    }
                    try {
                        entity.SystemRemoveCompleted(e);
                    }
// ReSharper disable EmptyGeneralCatchClause
                    catch {}
// ReSharper restore EmptyGeneralCatchClause
                }
                throw;
            }
        }