领域驱动设计之银行转账:Wow框架实战
生活随笔
收集整理的這篇文章主要介紹了
领域驱动设计之银行转账:Wow框架实战
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
銀行賬戶轉(zhuǎn)賬案例
銀行賬戶轉(zhuǎn)賬案例是一個經(jīng)典的領(lǐng)域驅(qū)動設(shè)計(DDD)應(yīng)用場景。接下來我們通過一個簡單的銀行賬戶轉(zhuǎn)賬案例,來了解如何使用 Wow 進(jìn)行領(lǐng)域驅(qū)動設(shè)計以及服務(wù)開發(fā)。
銀行轉(zhuǎn)賬流程
- 準(zhǔn)備轉(zhuǎn)賬(Prepare): 用戶發(fā)起轉(zhuǎn)賬請求,觸發(fā) Prepare 步驟。這個步驟會向源賬戶發(fā)送準(zhǔn)備轉(zhuǎn)賬的請求。
- 校驗余額(CheckBalance): 源賬戶在收到準(zhǔn)備轉(zhuǎn)賬請求后,會執(zhí)行校驗余額的操作,確保賬戶有足夠的余額進(jìn)行轉(zhuǎn)賬。
- 鎖定金額(LockAmount): 如果余額足夠,源賬戶會鎖定轉(zhuǎn)賬金額,防止其他操作干擾。
- 入賬(Entry): 接著,轉(zhuǎn)賬流程進(jìn)入到目標(biāo)賬戶,執(zhí)行入賬操作。
- 確認(rèn)轉(zhuǎn)賬(Confirm): 如果入賬成功,確認(rèn)轉(zhuǎn)賬;否則,執(zhí)行解鎖金額操作。
- 成功路徑(Success): 如果一切順利,完成轉(zhuǎn)賬流程。
- 失敗路徑(Fail): 如果入賬失敗,執(zhí)行解鎖金額操作,并處理失敗情況。
運行案例
- 運行 TransferExampleServer.java
- 查看 Swagger-UI : http://localhost:8080/swagger-ui.html
- 執(zhí)行 API 測試:Transfer.http
自動生成 API 端點
運行之后,訪問 Swagger-UI : http://localhost:8080/swagger-ui.html 。
該 RESTful API 端點是由 Wow 自動生成的,無需手動編寫。
模塊劃分
| 模塊 | 說明 |
|---|---|
| example-transfer-api | API 層,定義聚合命令(Command)、領(lǐng)域事件(Domain Event)以及查詢視圖模型(Query View Model),這個模塊充當(dāng)了各個模塊之間通信的“發(fā)布語言”。 |
| example-transfer-domain | 領(lǐng)域?qū)樱酆细蜆I(yè)務(wù)約束的實現(xiàn)。聚合根:領(lǐng)域模型的入口點,負(fù)責(zé)協(xié)調(diào)領(lǐng)域?qū)ο蟮牟僮鳌I(yè)務(wù)約束:包括驗證規(guī)則、領(lǐng)域事件的處理等。 |
| example-transfer-server | 宿主服務(wù),應(yīng)用程序的啟動點。負(fù)責(zé)整合其他模塊,并提供應(yīng)用程序的入口。涉及配置依賴項、連接數(shù)據(jù)庫、啟動 API 服務(wù) |
領(lǐng)域建模
賬戶聚合根
狀態(tài)聚合根(AccountState)與命令聚合根(Account)分離設(shè)計保證了在執(zhí)行命令過程中,不會修改狀態(tài)聚合根的狀態(tài)。
狀態(tài)聚合根(AccountState)建模
public class AccountState implements Identifier {
private final String id;
private String name;
/**
* 余額
*/
private long balanceAmount = 0L;
/**
* 已鎖定金額
*/
private long lockedAmount = 0L;
/**
* 賬號已凍結(jié)標(biāo)記
*/
private boolean frozen = false;
@JsonCreator
public AccountState(@JsonProperty("id") String id) {
this.id = id;
}
@NotNull
@Override
public String getId() {
return id;
}
public String getName() {
return name;
}
public long getBalanceAmount() {
return balanceAmount;
}
public long getLockedAmount() {
return lockedAmount;
}
public boolean isFrozen() {
return frozen;
}
void onSourcing(AccountCreated accountCreated) {
this.name = accountCreated.name();
this.balanceAmount = accountCreated.balance();
}
void onSourcing(AmountLocked amountLocked) {
balanceAmount = balanceAmount - amountLocked.amount();
lockedAmount = lockedAmount + amountLocked.amount();
}
void onSourcing(AmountEntered amountEntered) {
balanceAmount = balanceAmount + amountEntered.amount();
}
void onSourcing(Confirmed confirmed) {
lockedAmount = lockedAmount - confirmed.amount();
}
void onSourcing(AmountUnlocked amountUnlocked) {
lockedAmount = lockedAmount - amountUnlocked.amount();
balanceAmount = balanceAmount + amountUnlocked.amount();
}
void onSourcing(AccountFrozen accountFrozen) {
this.frozen = true;
}
}
命令聚合根(Account)建模
@StaticTenantId
@AggregateRoot
public class Account {
private final AccountState state;
public Account(AccountState state) {
this.state = state;
}
AccountCreated onCommand(CreateAccount createAccount) {
return new AccountCreated(createAccount.name(), createAccount.balance());
}
@OnCommand(returns = {AmountLocked.class, Prepared.class})
List<?> onCommand(Prepare prepare) {
checkBalance(prepare.amount());
return List.of(new AmountLocked(prepare.amount()), new Prepared(prepare.to(), prepare.amount()));
}
private void checkBalance(long amount) {
if (state.isFrozen()) {
throw new IllegalStateException("賬號已凍結(jié)無法轉(zhuǎn)賬.");
}
if (state.getBalanceAmount() < amount) {
throw new IllegalStateException("賬號余額不足.");
}
}
Object onCommand(Entry entry) {
if (state.isFrozen()) {
return new EntryFailed(entry.sourceId(), entry.amount());
}
return new AmountEntered(entry.sourceId(), entry.amount());
}
Confirmed onCommand(Confirm confirm) {
return new Confirmed(confirm.amount());
}
AmountUnlocked onCommand(UnlockAmount unlockAmount) {
return new AmountUnlocked(unlockAmount.amount());
}
AccountFrozen onCommand(FreezeAccount freezeAccount) {
return new AccountFrozen(freezeAccount.reason());
}
}
轉(zhuǎn)賬流程管理器(TransferSaga)
轉(zhuǎn)賬流程管理器(TransferSaga)負(fù)責(zé)協(xié)調(diào)處理轉(zhuǎn)賬的事件,并生成相應(yīng)的命令。
-
onEvent(Prepared): 訂閱轉(zhuǎn)賬已準(zhǔn)備就緒事件(Prepared),并生成入賬命令(Entry)。 -
onEvent(AmountEntered): 訂閱轉(zhuǎn)賬已入賬事件(AmountEntered),并生成確認(rèn)轉(zhuǎn)賬命令(Confirm)。 -
onEvent(EntryFailed): 訂閱轉(zhuǎn)賬入賬失敗事件(EntryFailed),并生成解鎖金額命令(UnlockAmount)。
@StatelessSaga
public class TransferSaga {
Entry onEvent(Prepared prepared, AggregateId aggregateId) {
return new Entry(prepared.to(), aggregateId.getId(), prepared.amount());
}
Confirm onEvent(AmountEntered amountEntered) {
return new Confirm(amountEntered.sourceId(), amountEntered.amount());
}
UnlockAmount onEvent(EntryFailed entryFailed) {
return new UnlockAmount(entryFailed.sourceId(), entryFailed.amount());
}
}
單元測試
internal class AccountKTest {
@Test
fun createAccount() {
aggregateVerifier<Account, AccountState>()
.given()
.`when`(CreateAccount("name", 100))
.expectEventType(AccountCreated::class.java)
.expectState {
assertThat(it.name, equalTo("name"))
assertThat(it.balanceAmount, equalTo(100))
}
.verify()
}
}
總結(jié)
以上是生活随笔為你收集整理的领域驱动设计之银行转账:Wow框架实战的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 打破16项AI性能记录!英伟达A100
- 下一篇: Welcome to YARP - 8.