//应用服务 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; }
}
|
问题:
为什么需要领域服务?如果我在领域对象中定义一个取款的方法,然后把领域服务的内容挪到取款方法里好像也可以。