领域服务,应用服务,领域方法的问题


//应用服务
public class WithDrawFacade {

public void execute(String acctNo, double amt) throws Exception {

Repository.beginTransaction();
Account acct = (Account) Repository.load(Account.class, acctNo);
// 创建领域服务
WithDrawService oper = ServiceFactory.createWithDrawOperation(acctNo,
"90001");
WithDrawRecord rec = (WithDrawRecord) oper.execute(acct, amt);
Repository.save(Account.class, acct);
Repository.save(WithDrawRecord.class, rec);
Repository.commitTransaction();

}

}


//领域服务
public class WithDrawService extends Service {

public Object execute(Account acct, Account cashAcct, double amt)
throws Exception {
// 限额判断
for (LimitRule limit : this.limits) {
limit.applyRules(amt);
}
// 手续费处理
double f = (Double) fee.applyRules(amt);
// 创建取款记录
WithDrawRecord rec = new WithDrawRecord(amt, (acct.getAcctNo()));
rec.setTransfee(f);
records.add(rec);
// 支付
acct.debit(amt);
// 领域方法
cashAcct.credit(amt);
// 领域方法
return rec;
}

}

问题:

为什么需要领域服务?如果我在领域对象中定义一个取款的方法,然后把领域服务的内容挪到取款方法里好像也可以。


如果是在两个账户之间转账,这涉及到两个账户,不是一个账户能完成,换句话说,转账这个动作已经跨越账户这个边界,变成一个比较长的交互过程。

建议参考:迪米特法则(Law of Demeter)与领域模型行为http://www.jdon.com/45378