Example #1
0
        public void Execute(List<System.Data.Objects.DataClasses.EntityObject> objects, Dictionary<System.Data.EntityKey, System.Data.EntityState> states)
        {
            foreach (EntityObject entityObject in objects)
            {
                foreach (Transaction transaction in _transactions)
                {
                    EntityState currentState = EntityState.Detached;

                    #region Get Current Object State
                    if (states.ContainsKey(entityObject.EntityKey))
                    {
                        if (states[entityObject.EntityKey] == System.Data.EntityState.Modified)
                        {
                            if (transaction.WhenUpdate)
                            {
                                currentState = EntityState.Modified;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (transaction.WhenDelete)
                            {
                                currentState = EntityState.Deleted;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    else
                    {
#warning detail的新增状态判断
                        if ((entityObject.EntityKey == null || (entityObject.EntityKey != null && entityObject.EntityState == EntityState.Added)) && transaction.WhenInsert)
                        {
                            currentState = EntityState.Added;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    #endregion

                    transaction.CurrentState = currentState;

                    transaction.CurrentObject = entityObject;

                    #region Before Transaction Event
                    EFTransactionBeforeTransEventArgs beforeArgs = new EFTransactionBeforeTransEventArgs(transaction);
                    OnBeforeTrans(beforeArgs);

                    if (beforeArgs.Cancel)
                    {
                        continue;
                    }
                    #endregion

                    if (CheckFieldsValidate(transaction))
                    {
                        #region Logic of transaction
                        List<TransKeyField> transKeyFields = this.GetTransKeyFieldsList(transaction, entityObject, currentState);
                        List<TransField> transFields = this.GetTransFieldList(transaction, entityObject, currentState);
                        bool hasRow = false;

                        switch (currentState)
                        {
                            case EntityState.Added:
                            case EntityState.Deleted:
                            case EntityState.Modified:
                                switch (transaction.TransMode)
                                {
                                    case TransMode.AlwaysAppend:
                                        #region AlwaysAppend
                                        if (currentState == EntityState.Modified)
                                        {
                                            this.AddObject(transaction, entityObject, currentState, transKeyFields, transFields, true, true, true);
                                        }
                                        this.AddObject(transaction, entityObject, currentState, transKeyFields, transFields, false, false, false);
                                        #endregion
                                        break;
                                    case TransMode.Exception:
                                    case TransMode.AutoAppend:
                                    case TransMode.Ignore:
                                        #region AutoAppend && Exception
                                        bool keyChanged = false;
                                        // 只有在Modified的情况下才有可能TransKeyFields被改变。
                                        if (currentState == EntityState.Modified)
                                        {
                                            keyChanged = this.CheckTransKeyFieldsIsOrNotChanged(transKeyFields);
                                        }

                                        if (keyChanged)
                                        {
                                            // Modified:TransKeyFields有被改变,两笔记录。
                                            // 1、
                                            EntityObject oldDestinationObject = this.GetDestinationObject(transaction, transKeyFields, currentState, true);
                                            this.UpdateObject(oldDestinationObject, transFields, currentState, true, false);
                                        }

                                        // 2、
                                        EntityObject destinationObject = this.GetDestinationObject(transaction, transKeyFields, currentState, false);
                                        if (destinationObject != null)
                                        {
                                            hasRow = true;
                                        }

                                        if (hasRow)
                                        {
                                            if (keyChanged)
                                            {
                                                UpdateObject(destinationObject, transFields, currentState, true, true);
                                            }
                                            else
                                            {
                                                UpdateObject(destinationObject, transFields, currentState, false, false);
                                            }

                                            #region Write back
                                            bool writeBack = this.CheckWriteBack(transaction.TransFields);
                                            if (writeBack)
                                            {
                                                if (currentState != EntityState.Deleted)
                                                {
                                                    this.WriteBack(transaction, entityObject, destinationObject, transaction.TransFields);
                                                }
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            switch (transaction.TransMode)
                                            {
                                                case TransMode.AutoAppend:
                                                    if (currentState != EntityState.Deleted)
                                                    {
                                                        this.AddObject(transaction, entityObject, currentState, transKeyFields, transFields, false, false, false);
                                                    }
                                                    else
                                                    {
                                                        this.AddObject(transaction, entityObject, currentState, transKeyFields, transFields, true, true, true);
                                                    }
                                                    break;
                                                case TransMode.Exception:
                                                    throw new ArgumentException(MessageHelper.EFTransactionMessage.NotExistRowInTable);
                                                case TransMode.Ignore:
                                                    break;
                                            }
                                        }
                                        #endregion
                                        break;
                                }


                                break;
                        }
                        #endregion
                    }
                }

                #region After Transaction Event
                OnAfterTrans(new EFTransactionAfterTransEventArgs());
                #endregion
            }
        }
Example #2
0
 protected void OnBeforeTrans(EFTransactionBeforeTransEventArgs value)
 {
     EFTransactionBeforeTransEventHandler handler = (EFTransactionBeforeTransEventHandler)Events[EventBeforeTrans];
     if ((handler != null) && (value is EFTransactionBeforeTransEventArgs))
     {
         handler(this, (EFTransactionBeforeTransEventArgs)value);
     }
 }