Beispiel #1
0
        private void ProcessItems(IList <Entity> entities)
        {
            if (entities.Count == 0)
            {
                return;
            }

            var entityType = entities[0].TypeInfo;
            var sequence   = entityType.GetRemovalAssociationSequence();

            if (sequence == null || sequence.Count == 0)
            {
                return;
            }

            ExecutePrefetchAction(entities);

            var referenceDescriptors = new List <ReferenceDescriptor>();

            foreach (var association in sequence)
            {
                if (association.OwnerType.UnderlyingType.IsAssignableFrom(entityType.UnderlyingType))
                {
                    foreach (var entity in entities)
                    {
                        var descriptor = new ReferenceDescriptor {
                            RemovingEntity = entity,
                            Processor      = GetProcessor(association.OnOwnerRemove.Value),
                            Association    = association,
                            References     = ReferenceFinder.GetReferencesFrom(entity, association),
                            IsOutgoing     = true
                        };
                        referenceDescriptors.Add(descriptor);
                    }
                }
                if (association.TargetType.UnderlyingType.IsAssignableFrom(entityType.UnderlyingType))
                {
                    foreach (var entity in entities)
                    {
                        var descriptor = new ReferenceDescriptor {
                            RemovingEntity = entity,
                            Processor      = GetProcessor(association.OnTargetRemove.Value),
                            Association    = association,
                            References     = ReferenceFinder.GetReferencesTo(entity, association),
                            IsOutgoing     = false
                        };
                        referenceDescriptors.Add(descriptor);
                    }
                }
            }

            if (Session.Handler.ExecutePrefetchTasks() == null)
            {
                Session.ExecuteUserDefinedDelayedQueries(false);
            }

            foreach (var container in referenceDescriptors)
            {
                var processor      = container.Processor;
                var association    = container.Association;
                var removingEntity = container.RemovingEntity;
                if (container.IsOutgoing)
                {
                    foreach (var reference in container.References.ToList())
                    {
                        processor.Process(Context, association, removingEntity, reference.ReferencedEntity, removingEntity, reference.ReferencedEntity);
                    }
                }
                else
                {
                    foreach (var reference in container.References.ToList())
                    {
                        processor.Process(Context, association, removingEntity, reference.ReferencingEntity, reference.ReferencingEntity, removingEntity);
                    }
                }
            }
        }