internal void RemoveOrCreateRemovedEntity(Type type, Key key, EntityRemoveReason reason) { // Checking for deleted entity with the same key var result = EntityStateCache[key, false]; if (result != null) { if (result.PersistenceState == PersistenceState.Removed) { return; } result.Entity.RemoveLaterInternal(reason); return; } EnforceChangeRegistrySizeLimit(); // Must be done before new entity registration result = new EntityState(this, key, null) { PersistenceState = PersistenceState.Removed }; EntityStateCache.Add(result); if (IsDebugEventLoggingEnabled) { OrmLog.Debug(Strings.LogSessionXCachingY, this, result); } }
internal void NotifyEntityRemoving(Entity entity, EntityRemoveReason reason) { if (EntityRemoving != null && AreNotificationsEnabled()) { EntityRemoving(this, new EntityRemovingEventArgs(entity, reason)); } }
internal void SystemBeforeRemove(EntityRemoveReason reason) { if (Session.IsDebugEventLoggingEnabled) { OrmLog.Debug(Strings.LogSessionXRemovingKeyY, Session, Key); } Session.SystemEvents.NotifyEntityRemoving(this, reason); using (Session.Operations.EnableSystemOperationRegistration()) { Session.Events.NotifyEntityRemoving(this, reason); if (Session.IsSystemLogicOnly) { return; } var subscriptionInfo = GetSubscription(EntityEventBroker.RemovingEntityEventKey); if (subscriptionInfo.Second != null) { ((Action <Key>)subscriptionInfo.Second) .Invoke(subscriptionInfo.First); } OnRemoving(); } }
public void Enqueue(IEnumerable <Entity> entities, EntityRemoveReason reason) { foreach (var group in entities.GroupBy(e => e.TypeInfo)) { var type = group.Key; if (types.Count > 0) { if (types.Peek() != type) { types.Enqueue(type); } } else { types.Enqueue(type); } if (!queue.TryGetValue(type, out var set1)) { set1 = new HashSet <Entity>(); queue.Add(type, set1); } foreach (var entity in group) { removeReasons[entity] = reason; _ = set1.Add(entity); } } }
public void Enqueue(Entity entity, EntityRemoveReason reason) { var type = entity.TypeInfo; if (types.Count > 0) { if (types.Peek() != type) { types.Enqueue(type); } } else { types.Enqueue(type); } if (queue.TryGetValue(type, out var set)) { _ = set.Add(entity); } else { set = new HashSet <Entity> { entity }; queue.Add(type, set); } removeReasons[entity] = reason; }
public void EnqueueForRemoval(IEnumerable <Entity> entities, EntityRemoveReason reason) { if (Context != null) { Context.Enqueue(entities, reason); } else { Remove(entities, reason); } }
internal void RemoveInternal(EntityRemoveReason reason) { Session.RemovalProcessor.Remove(EnumerableUtils.One(this), reason); }
internal void RemoveLaterInternal(EntityRemoveReason reason) { Session.RemovalProcessor.EnqueueForRemoval(EnumerableUtils.One(this), reason); }
// Constructors public EntityRemovingEventArgs(Entity entity, EntityRemoveReason reason) : base(entity) { Reason = reason; }
public void Remove(IEnumerable <Entity> entities, EntityRemoveReason reason) { ArgumentValidator.EnsureArgumentNotNull(entities, "entities"); var 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, reason); var isOperationStarted = false; while (!Context.QueueIsEmpty) { var entitiesForProcessing = Context.GatherEntitiesForProcessing(); foreach (var entity in entitiesForProcessing) { entity.SystemBeforeRemove(Context.GetRemoveReason(entity)); } 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; } }