Last active
June 2, 2017 09:58
-
-
Save michal-lipski/2f8f451308e34ffc4923096d33f7cf1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BankAccountA{ | |
void TransferMoneyTo(transactionToken,toAccountId,amount){ | |
if(this.HasPendingTransactions){ | |
throw new BusinessException("Transactions in process"); | |
} | |
if(availableBalance-amount<0){ | |
throw new BusinessException("..."); | |
} | |
DomainEvent e=new TransferMoneyToAccepted(....); | |
Apply(e); | |
Bus.Publish(e); | |
} | |
void Apply(TransferMoneyToAccepted e){ | |
this.HasPendingTransactions=true; | |
} | |
void CommitTransferMoneyTo(transactionToken){ | |
MoneyTransferToAccepted t=this.transactions.get<TransferMoneyToAccepted>(transactionToken); | |
if(t==null).... | |
DomainEvent e=new MoneyTransferToCommitted(...,this.balance-t.amount); | |
Apply(e); | |
Bus.Publish(e); | |
} | |
void Apply(MoneyTransferToCommitted e){ | |
this.HasPendingTransactions=false; | |
this.balance=e.balance; | |
..... | |
} | |
... | |
} | |
BankAccountB { | |
void TransferMoneyFrom(transactionToken, fromAccountId, amount) { | |
if (this.HasPendingTransactions) { | |
throw new BusinessException("Transactions in process"); | |
} | |
// some other business logic | |
DomainEvent e = new MoneyTransferFromAccepted(....); | |
Apply(e); | |
Bus.Publish(e); | |
} | |
void Apply(MoneyTransferFromAccepted e) { | |
this.HasPendingTransactions = true; | |
} | |
void CommitTransferMoneyFrom(transactionToken) { | |
MoneyTransferFromAccepted t = his.transactions.get<MoneyTransferFromAccepted>(transactionToken); | |
if (t == null) .... | |
DomainEvent e = new MoneyTransferToCommitted(transactionToken, this.balance + t.amount); | |
Apply(e); | |
Bus.Publish(e); | |
} | |
void Apply(MoneyTransferToCommitted e) { | |
this.HasPendingTransactions = false; | |
this.balance = e.balance; | |
} | |
.... | |
} | |
class MoneyTransferSaga:Saga<MoneyTransferSaga>, StartedBy<MoneyTransferToAccepted>, StartedBy<MoneyTransferFromAccepted>{ | |
void Handle(MoneyTransferFromAccepted e){ | |
this.moneyTransferFromAccepted=e; | |
if(this._moneyTransferToPending!=null){ | |
Bus.Send(new CommitMoneyTransferTo(...); | |
Bus.Send(new CommitMoneyTransferFrom(...); | |
this.MarkComplete(); | |
} | |
} | |
void Handle(MoneyTransferToAccepted e){ | |
this.MoneyTransferToAccepted=e; | |
if(this._moneyTransferFromPending!=null){ | |
Bus.Send(new CommitMoneyTransferTo(...); | |
Bus.Send(new CommitMoneyTransferFrom(...); | |
this.MarkComplete(); | |
} | |
} | |
void override TimeOut(){ | |
try{ | |
if(this._moneyTransferToPending!=null) | |
Bus.Send(newAbortMoneyTransferTo(...); | |
if(this._moneyTransferFromPending!=null) | |
Bus.Send(new AbortMoneyTransferFrom(...); | |
}catch(Exception e){ | |
....register failure launch red flag for manual correction. | |
} | |
this.MarkComplete(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment