/// <summary>
 /// Lança uma exception caso o resultado não seja satisfatório.
 /// </summary>
 /// <param name="result"></param>
 public static void ThrowInvalid(this Business.SaveResult result)
 {
     if (result != null && !result)
     {
         throw new Business.SaveResultException(result);
     }
 }
 /// <summary>
 /// Executa o save 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.SaveResult Save(this Data.IPersistenceSession session, params Func <Data.IPersistenceSession, Business.SaveResult>[] items)
 {
     items.Require("items").NotNull();
     Business.SaveResult result = null;
     foreach (var i in items)
     {
         if (i != null && !(result = i(session)))
         {
             return(result);
         }
     }
     return(new Business.SaveResult(true, null));
 }
Example #3
0
 /// <summary>
 /// Construtor padrão.
 /// </summary>
 /// <param name="result"></param>
 public SaveResultException(SaveResult result) : base(result.Message)
 {
 }
Example #4
0
 /// <summary>
 /// Construtor de deserialização.
 /// </summary>
 /// <param name="info"></param>
 /// <param name="context"></param>
 protected SaveResultException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
 {
     _saveResult = new SaveResult(false, MessageFormattable ?? Colosoft.MessageFormattable.Empty);
 }
Example #5
0
        /// <summary>
        /// Registra a entidade para ser apagada.
        /// </summary>
        /// <param name="session">Sessão onde a operação será realizada.</param>
        public override DeleteResult Delete(Data.IPersistenceSession session)
        {
            var result = OnDeleting();

            if (!result.Success)
            {
                return(result);
            }
            if (ExistsInStorage)
            {
                var actionId      = session.Delete(DataModel, DeleteActionCallback);
                var beforeSession = session.CreateBeforeSessionForAction(actionId);
                var afterSession  = session.CreateAfterSessionForAction(actionId);
                Domain.DomainEvents.Instance.GetEvent <EntityDeletingWithPersistenceSessionEvent>().Publish(new EntityDeletingWithPersistenceSessionEventArgs(this, session, beforeSession, afterSession));
                var beforeLinksAccessors = Loader.GetLinksAccessors(EntityChildSavePriority.BeforeEntity);
                var linksAccessors       = Loader.GetLinksAccessors(EntityChildSavePriority.AfterEntity);
                foreach (var i in beforeLinksAccessors)
                {
                    var entity = i.Get(this);
                    if (entity != null)
                    {
                        SaveResult saveResult = null;
                        if (entity is ISaveOperationsContainer)
                        {
                            var container = (ISaveOperationsContainer)entity;
                            saveResult = container.ProcessUpdateOperations(beforeSession);
                            if (saveResult.Success)
                            {
                                saveResult = container.ProcessInsertOperations(beforeSession);
                            }
                        }
                        else
                        {
                            saveResult = entity.Save(beforeSession);
                        }
                        if (!saveResult.Success)
                        {
                            return(new DeleteResult(false, saveResult.Message));
                        }
                        if (entity is IEntitySavePersistenceSessionObserver)
                        {
                            ((IEntitySavePersistenceSessionObserver)entity).Register(beforeSession);
                        }
                    }
                }
                foreach (var i in Loader.GetChildrenAccessors().Reverse())
                {
                    var entityRemoved = GetSingleChildRemoved(i.Name);
                    if (entityRemoved != null)
                    {
                        var deleteResult = entityRemoved.Delete(beforeSession);
                        if (!deleteResult.Success)
                        {
                            return(deleteResult);
                        }
                    }
                    var child = i.Get(this);
                    if (child != null)
                    {
                        result = child.Delete(beforeSession);
                        if (!result.Success)
                        {
                            return(result);
                        }
                    }
                }
                var references = Loader.GetReferences().Where(f => this.IsReferenceInitialized(f.Name));
                foreach (var item in new Tuple <Data.IPersistenceSession, EntityChildSavePriority>[] {
                    new Tuple <Data.IPersistenceSession, EntityChildSavePriority>(beforeSession, EntityChildSavePriority.BeforeEntity)
                })
                {
                    foreach (var i in references.Where(f => f.SavePriority == item.Item2))
                    {
                        var entity = i.ParentValueGetter(this);
                        Colosoft.Business.SaveResult saveResult = null;
                        if (entity != null && entity.IsChanged)
                        {
                            if (entity is ISaveOperationsContainer)
                            {
                                var container = (ISaveOperationsContainer)entity;
                                saveResult = container.ProcessUpdateOperations(item.Item1);
                                if (result.Success)
                                {
                                    saveResult = container.ProcessInsertOperations(item.Item1);
                                }
                            }
                            else
                            {
                                saveResult = entity.Save(item.Item1);
                            }
                            if (!result.Success)
                            {
                                return(new DeleteResult(false, saveResult.Message));
                            }
                            if (entity is IEntitySavePersistenceSessionObserver)
                            {
                                ((IEntitySavePersistenceSessionObserver)entity).Register(afterSession);
                            }
                        }
                    }
                }
                foreach (var i in linksAccessors)
                {
                    var entity = i.Get(this);
                    if (entity != null)
                    {
                        SaveResult saveResult = null;
                        if (entity is ISaveOperationsContainer)
                        {
                            var container = (ISaveOperationsContainer)entity;
                            saveResult = container.ProcessUpdateOperations(afterSession);
                            if (result.Success)
                            {
                                saveResult = container.ProcessInsertOperations(afterSession);
                            }
                        }
                        else
                        {
                            saveResult = entity.Save(afterSession);
                        }
                        if (!saveResult.Success)
                        {
                            return(new DeleteResult(false, saveResult.Message));
                        }
                        if (entity is IEntitySavePersistenceSessionObserver)
                        {
                            ((IEntitySavePersistenceSessionObserver)entity).Register(afterSession);
                        }
                    }
                }
            }
            return(result);
        }