2013-07-29 16:16 "@fengweili
"的内容
做一个转账的的应用,是这样 ...
都不是,大概是这样:
1. AngularJS先GET获得当前帐号信息
2.根据帐号信息中提供的URL发出转账的POST命令:
POST /transactions
然后加入参数 from=1&to=2&amount=500.00
见:http://www.jdon.com/41716
3.第二步的POST命令直接递交到转账服务,transactionService.
在转账服务中,有二种实现方式:
1. DCI+面向函数风格,直接在转账服务的方法中实现,将源账户和目标帐号看成两个角色TransferMoneySourceAccount和TransferMoneyDestinationAccount,需要通过事务机制。见:http://www.jdon.com/37976
2.DDD聚合体+EventSourcing方式, 转账服务委托聚合根实现,TransactionAggregate作为聚合根实体,转账是这个实体的一个行为方法,转账命令到激活这个方法,在这个方法内部将产生一个转账事件。见:
http://simon-says-architecture.com/2013/03/07/modelling-accounting-ledger-event-driven/
http://simon-says-architecture.com/2013/03/22/modelling-accounting-ledger-event-driven-2/
更正一下,我前面提到一个命令一个事件,是指一个命令对应上游事件,一旦上游事件进入聚合根内部,变成很多事件流分支,比如
var tx = new Transaction();
tx.Post(amount, fromAccount, toAccount);
transactionRepository.Store(tx);
<p>
|
这是调用Transaction这个聚合根的post方法。方法内部代码:
public void Post(decimal amount, string fromAccount, string toAccount)
{
this.Apply(new AccountDebited(amount, fromAccount));
this.Apply(new AccountCredited(amount, toAccount));
}
<p>
|
将转账命令(转账上游事件)分为两个事件,AccountDebited和AccountCredited,账户借款和账户贷款,这样保证借贷平衡。然后根据借贷状态切换,还有更多子状态,EventStore应该是这些和状态直接有关的事件,间接有关事件没有必要记录,这样在事件回放时才能重现状态真实改变历史。
[该贴被banq于2013-07-30 17:08修改过]