Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="action"></param>
        /// <returns>如果延迟执行,返回true</returns>
        private void ProcessAction(ScheduledAction action)
        {
            if (this._transactionStatus == TransactionStatus.Delay)
            {
                _actions.Add(action);//若处于延迟模式的事务中,那么将该操作暂存
                return;
            }

            if (this._transactionStatus == TransactionStatus.Timely)
            {
                //若已经开启全局事务,直接执行
                _actions.Add(action); //直接执行也要加入到actions集合中
                ExecuteAction(action);
                return;
            }

            if (this._transactionStatus == TransactionStatus.None)
            {
                //没有开启事务,立即执行
                using (ITransactionManager manager = GetTransactionManager())
                {
                    manager.Begin();
                    ExecuteAction(action);
                    //提交事务
                    RaisePreCommit(action);
                    manager.Commit();
                    RaiseCommitted(action);
                }
                return;
            }
        }
Пример #2
0
        /// <summary>
        /// 执行计划
        /// </summary>
        /// <param name="action"></param>
        private void ExecuteAction(ScheduledAction action)
        {
            if (action.Expired)
            {
                return;
            }

            action.Target.LoadState();
            this.ValidateAction(action);

            var repository = action.Repository;

            switch (action.Type)
            {
            case ScheduledActionType.Create:
                repository.PersistAdd(action.Target);
                action.Target.MarkClean();
                break;

            case ScheduledActionType.Update:
                repository.PersistUpdate(action.Target);
                action.Target.MarkClean();
                break;

            case ScheduledActionType.Delete:
                repository.PersistDelete(action.Target);
                action.Target.MarkDirty();
                break;
            }

            action.MarkExpired();
        }
Пример #3
0
 private void DisposeSchedule()
 {
     foreach (var action in _actions)
     {
         ScheduledAction.Return(action);
     }
     _actions.Clear();
 }
Пример #4
0
        /// <summary>
        /// 检验执行的计划
        /// </summary>
        /// <param name="action"></param>
        private void ValidateAction(ScheduledAction action)
        {
            if (action.Target.IsEmpty())
            {
                throw new ActionTargetIsEmptyException("对象为空,不能执行持久化操作!对象类型:" + action.Target.GetType().FullName);
            }

            if (action.Type == ScheduledActionType.Delete)
            {
                return;                                            //删除操作,不需要验证固定规则
            }
            ValidationResult result = action.Validate();

            if (!result.IsSatisfied)
            {
                throw new ValidationException(result);
            }
        }
Пример #5
0
        private void RaiseCommitted(ScheduledAction action)
        {
            switch (action.Type)
            {
            case ScheduledActionType.Create:
                action.Target.OnAddCommitted();
                StatusEvent.Execute(StatusEventType.AddCommitted, action.Target);
                break;

            case ScheduledActionType.Update:
                action.Target.OnUpdateCommitted();
                StatusEvent.Execute(StatusEventType.UpdateCommitted, action.Target);
                break;

            case ScheduledActionType.Delete:
                action.Target.OnDeleteCommitted();
                StatusEvent.Execute(StatusEventType.DeleteCommitted, action.Target);
                break;
            }
        }
Пример #6
0
 public void RegisterDeleted <T>(T item, IPersistRepository repository) where T : IAggregateRoot
 {
     ProcessAction(ScheduledAction.Borrow(item, repository, ScheduledActionType.Delete));
     item.SaveState();
     item.MarkDirty();//无论是延迟执行,还是立即执行,我们都需要提供统一的状态给领域层使用
 }
Пример #7
0
 public static void Return(ScheduledAction action)
 {
     _pool.Return(action);
 }