之前我们建立了UI和Application Service,UI是HTML页面,Application Service是Spring boot 的REST Controller。Application Service作为UI和领域模型的介质存在,不包含业务逻辑,仅验证输入,充当沟通桥梁。Controller将业务逻辑委托给实体,不仅是领域模型,它也能和其它组件交互,如Repositories仓库,这部分将在本章后续部分介绍。还有一点,确定代码的正确位置也很重要,比如要更人性化地显示金额,我们可以选择重写Money地toString方法:
@Override
public String toString() {
if (getAmount() < 1)
return "¢" + getAmount() * 100;
return "$" + getAmount();
}
如果要实现投币金额和账户金额,可以选择在Application Service中实现:
public Money getWholeMoney(SnackMachine snackMachine) {
return Money.add(snackMachine.getMoneyInside(), snackMachine.getMoneyInTransaction());
}
区分地关键是:如果之和UI有关则放于Application Service,反之,编写在领域模型中。