/// <summary> /// Lança uma exception caso o resultado não seja satisfatório. /// </summary> /// <param name="result"></param> public static void ThrowInvalid(this Business.DeleteResult result) { if (result != null && !result) { throw new Business.DeleteResultException(result); } }
/// <summary> /// Executa o delete dos itens informados. /// </summary> /// <param name="session"></param> /// <param name="items">Referências dos métodos save que deverão ser acionados.</param> /// <returns></returns> public static Business.DeleteResult DeleteItems(this Data.IPersistenceSession session, params Func <Data.IPersistenceSession, Business.DeleteResult>[] items) { items.Require("items").NotNull(); Business.DeleteResult result = null; foreach (var i in items) { if (i != null && !(result = i(session))) { return(result); } } return(new Business.DeleteResult(true, null)); }
/// <summary> /// Construtor de deserialização. /// </summary> /// <param name="info"></param> /// <param name="context"></param> protected DeleteResultException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { _deleteResult = new DeleteResult(false, MessageFormattable ?? Colosoft.MessageFormattable.Empty); }
/// <summary> /// Construtor padrão. /// </summary> /// <param name="result"></param> public DeleteResultException(DeleteResult result) : base(result.Message) { }
/// <summary> /// Persiste a entidade no banco de dados. /// </summary> /// <param name="session"></param> public virtual SaveResult Persist(Data.IPersistenceSession session) { if (!IsChanged && ExistsInStorage) { return(new SaveResult(true, null)); } var result = OnSaving(); if (!result.Success) { return(result); } var actionId = 0; DeleteResult deleteResult = null; Data.IPersistenceSession beforeSession = null; Data.IPersistenceSession afterSession = null; if (!ExistsInStorage) { actionId = session.Insert(DataModel, SaveActionCallback); } else { var changeProperties = ChangedProperties.ToArray(); if (changeProperties.Length > 0) { actionId = session.Update(DataModel, SaveActionCallback, changeProperties); } } _lastActionId = actionId; if (actionId > 0) { beforeSession = session.CreateBeforeSessionForAction(actionId); afterSession = session.CreateAfterSessionForAction(actionId); } else { beforeSession = session; afterSession = session; } Colosoft.Domain.DomainEvents.Instance.GetEvent <EntityPersistingEvent>().Publish(new EntityPersistingEventArgs(this, session, beforeSession, afterSession)); var references = Loader.GetReferences().Where(f => this.IsReferenceInitialized(f.Name)); var beforeLinksAccessors = Loader.GetLinksAccessors(EntityChildSavePriority.BeforeEntity); var beforeChildrenAccessors = Loader.GetChildrenAccessors(EntityChildSavePriority.BeforeEntity); var linksAccessors = Loader.GetLinksAccessors(EntityChildSavePriority.AfterEntity); var childrenAccessors = Loader.GetChildrenAccessors(EntityChildSavePriority.AfterEntity); var deleteProcessing = false; if (!session.Root.Parameters.Contains("DeleteOperationsProcessing")) { session.Root.Parameters["DeleteOperationsProcessing"] = true; deleteProcessing = true; deleteResult = ProcessDeleteOperations(beforeSession); if (!deleteResult.Success) { return(new SaveResult(deleteResult.Success, deleteResult.Message)); } } foreach (var i in beforeLinksAccessors) { var entity = i.Get(this); if (entity != null) { if (entity is ISaveOperationsContainer) { var container = (ISaveOperationsContainer)entity; result = container.ProcessUpdateOperations(beforeSession); if (result.Success) { result = container.ProcessInsertOperations(beforeSession); } } else { result = entity.Save(beforeSession); } if (!result.Success) { return(result); } if (entity is IEntitySavePersistenceSessionObserver) { ((IEntitySavePersistenceSessionObserver)entity).Register(beforeSession); } } } foreach (var i in beforeChildrenAccessors) { var entity = i.Get(this); var entityRemoved = GetSingleChildRemoved(i.Name); if (entityRemoved != null) { deleteResult = entityRemoved.Delete(beforeSession); if (!deleteResult.Success) { return(new SaveResult(deleteResult.Success, deleteResult.Message)); } } if (entity != null) { if (entity is ISaveOperationsContainer) { var container = (ISaveOperationsContainer)entity; result = container.ProcessUpdateOperations(beforeSession); if (result.Success) { result = container.ProcessInsertOperations(beforeSession); } } else { result = entity.Save(beforeSession); } if (!result.Success) { return(result); } if (entity is IEntitySavePersistenceSessionObserver) { ((IEntitySavePersistenceSessionObserver)entity).Register(beforeSession); } } } foreach (var i in references.Where(f => f.SavePriority == EntityChildSavePriority.BeforeEntity)) { var entity = i.ParentValueGetter(this); if (entity != null && entity.IsChanged) { if (entity is ISaveOperationsContainer) { var container = (ISaveOperationsContainer)entity; result = container.ProcessUpdateOperations(beforeSession); if (result.Success) { result = container.ProcessInsertOperations(beforeSession); } } else { result = entity.Save(beforeSession); } if (!result.Success) { return(result); } if (entity is IEntitySavePersistenceSessionObserver) { ((IEntitySavePersistenceSessionObserver)entity).Register(beforeSession); } } } foreach (var i in linksAccessors) { var entity = i.Get(this); if (entity != null) { if (entity is ISaveOperationsContainer) { var container = (ISaveOperationsContainer)entity; result = container.ProcessUpdateOperations(afterSession); if (result.Success) { result = container.ProcessInsertOperations(afterSession); } } else { result = entity.Save(afterSession); } if (!result.Success) { return(result); } if (entity is IEntitySavePersistenceSessionObserver) { ((IEntitySavePersistenceSessionObserver)entity).Register(afterSession); } } } foreach (var i in childrenAccessors) { var entity = i.Get(this); var entityRemoved = GetSingleChildRemoved(i.Name); if (entityRemoved != null) { deleteResult = entityRemoved.Delete(afterSession); if (!deleteResult.Success) { return(new SaveResult(deleteResult.Success, deleteResult.Message)); } } if (entity != null) { if (entity is ISaveOperationsContainer) { var container = (ISaveOperationsContainer)entity; result = container.ProcessUpdateOperations(afterSession); if (result.Success) { result = container.ProcessInsertOperations(afterSession); } } else { result = entity.Save(afterSession); } if (!result.Success) { return(result); } if (entity is IEntitySavePersistenceSessionObserver) { ((IEntitySavePersistenceSessionObserver)entity).Register(afterSession); } } } foreach (var i in references.Where(f => f.SavePriority == EntityChildSavePriority.AfterEntity)) { var entity = i.ParentValueGetter(this); if (entity != null && entity.IsChanged) { if (entity is ISaveOperationsContainer) { var container = (ISaveOperationsContainer)entity; result = container.ProcessUpdateOperations(afterSession); if (result.Success) { result = container.ProcessInsertOperations(afterSession); } } else { result = entity.Save(afterSession); } if (!result.Success) { return(result); } if (entity is IEntitySavePersistenceSessionObserver) { ((IEntitySavePersistenceSessionObserver)entity).Register(afterSession); } } } if (deleteProcessing) { session.Root.Parameters.Remove("DeleteOperationsProcessing"); } return(result); }
/// <summary> /// Processa as operações de delete. /// </summary> /// <param name="session"></param> /// <returns></returns> public DeleteResult ProcessDeleteOperations(Colosoft.Data.IPersistenceSession session) { var beforeLinksAccessors = Loader.GetLinksAccessors(EntityChildSavePriority.BeforeEntity); var beforeChildrenAccessors = Loader.GetChildrenAccessors(EntityChildSavePriority.BeforeEntity); var linksAccessors = Loader.GetLinksAccessors(EntityChildSavePriority.AfterEntity); var childrenAccessors = Loader.GetChildrenAccessors(EntityChildSavePriority.AfterEntity); var saveOperationsContainers = GetEntities(this, linksAccessors.Reverse().Concat(beforeLinksAccessors.Reverse())).Where(f => f is IDeleteOperationsContainer).Select(f => (IDeleteOperationsContainer)f); DeleteResult deleteResult = null; foreach (var i in childrenAccessors.Reverse()) { var entityRemoved = GetSingleChildRemoved(i.Name); if (entityRemoved != null) { deleteResult = entityRemoved.Delete(session); if (!deleteResult.Success) { return(deleteResult); } continue; } var entity = i.Get(this); if (entity is IDeleteOperationsContainer) { deleteResult = ((IDeleteOperationsContainer)entity).ProcessDeleteOperations(session); if (!deleteResult) { return(deleteResult); } } } foreach (var i in beforeChildrenAccessors.Reverse()) { var entityRemoved = GetSingleChildRemoved(i.Name); if (entityRemoved != null) { deleteResult = entityRemoved.Delete(session); if (!deleteResult.Success) { return(deleteResult); } continue; } var entity = i.Get(this); if (entity is IDeleteOperationsContainer) { deleteResult = ((IDeleteOperationsContainer)entity).ProcessDeleteOperations(session); if (!deleteResult) { return(deleteResult); } } } foreach (var entity in saveOperationsContainers) { deleteResult = entity.ProcessDeleteOperations(session); if (!deleteResult.Success) { return(deleteResult); } } return(new DeleteResult(true, null)); }