Esempio n. 1
0
        private void Handle(TransferTransactionStartedEvent evnt)
        {
            _transactionInfo = evnt.TransactionInfo;
            Status           = TransactionStatus.Started;
            var participants = new TransactionParticipantInfo[] {
                new TransactionParticipantInfo(_transactionInfo.SourceAccountId, (byte)AggregateRootTypes.BankAccount),
                new TransactionParticipantInfo(_transactionInfo.TargetAccountId, (byte)AggregateRootTypes.BankAccount),
            };

            HandleTransactionStartedEvent((byte)TransactionTypes.TransferTransaction, participants);
        }
Esempio n. 2
0
        private void Handle(DepositTransactionStartedEvent evnt)
        {
            _accountId = evnt.AccountId;
            _amount    = evnt.Amount;
            Status     = TransactionStatus.Started;
            var participants = new TransactionParticipantInfo[] {
                new TransactionParticipantInfo(evnt.AccountId, (byte)AggregateRootTypes.BankAccount)
            };

            HandleTransactionStartedEvent((byte)TransactionTypes.DepositTransaction, participants);
        }
Esempio n. 3
0
 /// <summary>
 /// Handle RolledbackParticipantAdded event
 /// </summary>
 /// <param name="transactionParticipant">事务参与方信息</param>
 protected void HandleRolledbackParticipantAddedEvent(TransactionParticipantInfo transactionParticipant)
 {
     _rolledbackTransactionParticipants.Add(transactionParticipant);
 }
Esempio n. 4
0
 /// <summary>
 /// Handle CommittedParticipantAdded event
 /// </summary>
 /// <param name="transactionParticipant">事务参与方信息</param>
 protected void HandleCommittedParticipantAddedEvent(TransactionParticipantInfo transactionParticipant)
 {
     _committedTransactionParticipants.Add(transactionParticipant);
 }
Esempio n. 5
0
 /// <summary>
 /// Handle PreCommitFailParticipantAdded event
 /// </summary>
 /// <param name="transactionId">事务ID</param>
 /// <param name="transactionParticipant">事务参与方信息</param>
 protected void HandlePreCommitFailParticipantAddedEvent(string transactionId, TransactionParticipantInfo transactionParticipant)
 {
     CurrentTransactionId = transactionId;
     _preCommitFailTransactionParticipants.Add(transactionParticipant);
 }
Esempio n. 6
0
 /// <summary>
 /// Create RolledbackParticipantAdded event
 /// </summary>
 /// <param name="transactionId">事务ID</param>
 /// <param name="transactionType">事务类型</param>
 /// <param name="participantInfo"></param>
 protected abstract TransactionInitiatorRolledbackParticipantAddedBase <TTransactionInitiator, TAggregateRootId> CreateRolledbackParticipantAddedEvent(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo);
Esempio n. 7
0
 /// <summary>
 /// Create PreCommitFailParticipantAdded event
 /// </summary>
 /// <param name="transactionId">事务ID</param>
 /// <param name="transactionType">事务类型</param>
 /// <param name="participantInfo"></param>
 protected abstract TransactionInitiatorPreCommitFailedParticipantAddedBase <TTransactionInitiator, TAggregateRootId> CreatePreCommitFailParticipantAddedEvent(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo);
Esempio n. 8
0
        /// <summary>
        /// 添加已回滚的参与者(依次发布 RolledbackParticipantAdded 、 TransactionCompleted 事件)
        /// </summary>
        /// <param name="transactionId">事务ID</param>
        /// <param name="transactionType">事务类型</param>
        /// <param name="participantInfo"></param>
        public void AddRolledbackParticipant(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo)
        {
            if (participantInfo == null)
            {
                throw new ArgumentNullException(nameof(participantInfo));
            }
            if (string.IsNullOrEmpty(transactionId))
            {
                throw new ArgumentNullException(nameof(transactionId));
            }
            if (!IsTransactionProcessing)
            {
                throw new ApplicationException($"Initiator {Id} is not in transaction, couldn't execute AddCommittedParticipant command.");
            }
            if (transactionType != CurrentTransactionType)
            {
                throw new ApplicationException($"Initiator {Id}'s transaction type {transactionType} is not same as {CurrentTransactionType}");
            }
            if (!string.IsNullOrEmpty(CurrentTransactionId) && transactionId != CurrentTransactionId)
            {
                throw new ApplicationException($"Initiator {Id}'s transaction id {transactionId} is not same as {CurrentTransactionId}");
            }
            if (_committedTransactionParticipants.Count > 0 && participantInfo.IsParticipantAlreadyExists(_committedTransactionParticipants))
            {
                return;
            }
            if (_rolledbackTransactionParticipants.Count > 0 && participantInfo.IsParticipantAlreadyExists(_rolledbackTransactionParticipants))
            {
                return;
            }
            if (!IsAllPreCommitParticipantAdded())
            {
                throw new ApplicationException("Initiator {Id} didn't received all PreCommit participant, couldn't execute AddRolledbackParticipant command.");
            }
            if (_allTransactionParticipants == null || !_allTransactionParticipants.Any(w => w.ParticipantId == participantInfo.ParticipantId))
            {
                return;
            }

            ApplyEvent(CreateRolledbackParticipantAddedEvent(transactionId, transactionType, participantInfo));
            if (IsAllCommittedAndRolledbackParticipantAdded())
            {
                // 所有参与者的提交和回滚都已处理
                ApplyEvent(CreateTransactionCompletedEvent(transactionId, transactionType, _rolledbackTransactionParticipants.Count == 0));
            }
        }
Esempio n. 9
0
        /// <summary>
        /// 添加预提交失败的参与者(依次发布 PreCommitFailParticipantAdded 、 AnyParticipantPreCommitFailed 事件)
        /// </summary>
        /// <param name="transactionId">事务ID</param>
        /// <param name="transactionType">事务类型</param>
        /// <param name="participantInfo"></param>
        public void AddPreCommitFailedParticipant(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo)
        {
            if (participantInfo == null)
            {
                throw new ArgumentNullException(nameof(participantInfo));
            }
            if (string.IsNullOrEmpty(transactionId))
            {
                throw new ArgumentNullException(nameof(transactionId));
            }
            if (!IsTransactionProcessing)
            {
                throw new ApplicationException($"Initiator {Id} is not in transaction, couldn't execute AddPreCommitFailedParticipant command.");
            }
            if (transactionType != CurrentTransactionType)
            {
                throw new ApplicationException($"Initiator {Id}'s transaction type {transactionType} is not same as {CurrentTransactionType}");
            }
            if (!string.IsNullOrEmpty(CurrentTransactionId) && transactionId != CurrentTransactionId)
            {
                throw new ApplicationException($"Initiator {Id}'s transaction id {transactionId} is not same as {CurrentTransactionId}");
            }
            if (_preCommitSuccessTransactionParticipants.Count > 0 && participantInfo.IsParticipantAlreadyExists(_preCommitSuccessTransactionParticipants))
            {
                return;
            }
            if (_preCommitFailTransactionParticipants.Count > 0 && participantInfo.IsParticipantAlreadyExists(_preCommitFailTransactionParticipants))
            {
                return;
            }
            if (_allTransactionParticipants == null || !_allTransactionParticipants.Any(w => w.ParticipantId == participantInfo.ParticipantId))
            {
                return;
            }

            ApplyEvent(CreatePreCommitFailParticipantAddedEvent(transactionId, transactionType, participantInfo));
            if (IsAllPreCommitParticipantAdded())
            {
                // 所有预提交已添加
                ApplyEvent(CreateAnyParticipantPreCommitFailedEvent(transactionId, transactionType, _preCommitSuccessTransactionParticipants));
                if (IsAllPreCommitParticipantAddedAndFail())
                {
                    // 所有预提交已添加且都失败,事务直接完成
                    ApplyEvent(CreateTransactionCompletedEvent(transactionId, transactionType, false));
                }
            }
        }
Esempio n. 10
0
 protected override TransactionInitiatorRolledbackParticipantAddedBase <DepositTransaction, string> CreateRolledbackParticipantAddedEvent(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo)
 {
     //TODO: 存款,假定不存在需要回滚的场景
     throw new NotImplementedException();
 }
Esempio n. 11
0
 protected override TransactionInitiatorCommittedParticipantAddedBase <DepositTransaction, string> CreateCommittedParticipantAddedEvent(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo)
 {
     return(new DepositTransactionCommittedParticipantAddedEvent(participantInfo.ParticipantId, transactionId));
 }
 /// <summary>
 /// 已回滚的事务参与方已添加事件基类
 /// </summary>
 /// <param name="transactionId">事务ID</param>
 /// <param name="transactionType">事务类型</param>
 /// <param name="transactionParticipant">事务参与方信息</param>
 protected TransactionInitiatorRolledbackParticipantAddedBase(string transactionId, byte transactionType, TransactionParticipantInfo transactionParticipant)
 {
     TransactionId          = transactionId;
     TransactionType        = transactionType;
     TransactionParticipant = transactionParticipant;
 }
Esempio n. 13
0
 /// <summary>
 /// 预提交失败的事务参与方已添加事件基类
 /// </summary>
 /// <param name="transactionId">事务ID</param>
 /// <param name="transactionType">事务类型</param>
 /// <param name="transactionParticipant">事务参与方信息</param>
 protected TransactionInitiatorPreCommitFailedParticipantAddedBase(string transactionId, byte transactionType, TransactionParticipantInfo transactionParticipant)
 {
     TransactionId          = transactionId;
     TransactionType        = transactionType;
     TransactionParticipant = transactionParticipant;
 }
Esempio n. 14
0
 protected override TransactionInitiatorRolledbackParticipantAddedBase <TransferTransaction, string> CreateRolledbackParticipantAddedEvent(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo)
 {
     return(new TransferTransactionRolledbackParticipantAddedEvent(participantInfo.ParticipantId, transactionId));
 }
Esempio n. 15
0
 protected override TransactionInitiatorPreCommitFailedParticipantAddedBase <TransferTransaction, string> CreatePreCommitFailParticipantAddedEvent(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo)
 {
     return(new TransferTransactionPreCommitFailedParticipantAdded(participantInfo.ParticipantId, transactionId));
 }
Esempio n. 16
0
        protected override TransactionInitiatorPreCommitSucceedParticipantAddedBase <TransferTransaction, string> CreatePreCommitSuccessParticipantAddedEvent(string transactionId, byte transactionType, TransactionParticipantInfo participantInfo)
        {
            var preparationType = _transactionInfo.SourceAccountId == participantInfo.ParticipantId ? PreparationTypes.DebitPreparation : PreparationTypes.CreditPreparation;

            return(new TransferTransactionPreCommitSucceedParticipantAdded(participantInfo.ParticipantId, transactionId, preparationType));
        }