架构与系统设计 / 系统课时
应用层怎样协调多个领域对象
应用层怎样协调多个领域对象
一次业务用例往往不只碰一个对象:应用层需要加载聚合、调用领域行为、保存结果,并在失败时结束流程。本节用“订单预留库存”串起完整调用链,重点看 Application Service 如何负责协调,而不把库存判断和订单状态规则写进自己。
学习目标
- 说明 Application Service 在多对象用例中的协调职责
- 区分应用层流程控制与领域对象业务规则
- 能按照加载、调用、持久化的顺序组织一次完整用例
从用例边界开始
应用服务的入口通常对应一个明确用例,例如“为订单预留库存”。它接收的是完成用例所需的参数,而不是把 HTTP、数据库连接或 ORM 实体直接暴露给领域层。
应用层最重要的工作是安排顺序:先从 Repository 取得需要参与本次用例的聚合,再调用它们公开的领域行为,最后保存发生变化的聚合。它知道“先做什么、后做什么”,但不负责回答“库存是否足够”“订单当前状态是否允许预留”这类业务问题。

用完整代码观察协调过程
下面的 Java 示例使用内存 Repository,便于直接运行。真正项目中 Repository 的实现通常位于基础设施层,但应用服务只需要依赖抽象,不需要知道数据最终存进哪种数据库。
import java.util.HashMap;
import java.util.Map;
public class Main {
record ReserveInventoryCommand(String orderId) {}
static final class Order {
private final String id;
private final String sku;
private final int quantity;
private String status = "CREATED";
Order(String id, String sku, int quantity) {
this.id = id;
this.sku = sku;
this.quantity = quantity;
}
String id() { return id; }
String sku() { return sku; }
int quantity() { return quantity; }
String status() { return status; }
void markReserved() {
if (!"CREATED".equals(status)) {
throw new IllegalStateException("当前订单状态不能预留库存");
}
status = "RESERVED";
}
}
static final class Inventory {
private final String sku;
private int available;
Inventory(String sku, int available) {
this.sku = sku;
this.available = available;
}
String sku() { return sku; }
int available() { return available; }
void reserve(int quantity) {
if (quantity <= 0) {
throw new IllegalArgumentException("预留数量必须大于 0");
}
if (available < quantity) {
throw new IllegalStateException("库存不足");
}
available -= quantity;
}
}
interface OrderRepository {
Order get(String orderId);
void save(Order order);
}
interface InventoryRepository {
Inventory getBySku(String sku);
void save(Inventory inventory);
}
static final class InMemoryOrderRepository implements OrderRepository {
private final Map<String, Order> data = new HashMap<>();
void add(Order order) { data.put(order.id(), order); }
public Order get(String orderId) {
Order order = data.get(orderId);
if (order == null) throw new IllegalArgumentException("订单不存在");
return order;
}
public void save(Order order) {
data.put(order.id(), order);
}
}
static final class InMemoryInventoryRepository implements InventoryRepository {
private final Map<String, Inventory> data = new HashMap<>();
void add(Inventory inventory) { data.put(inventory.sku(), inventory); }
public Inventory getBySku(String sku) {
Inventory inventory = data.get(sku);
if (inventory == null) throw new IllegalArgumentException("库存记录不存在");
return inventory;
}
public void save(Inventory inventory) {
data.put(inventory.sku(), inventory);
}
}
static final class ReserveInventoryApplicationService {
private final OrderRepository orders;
private final InventoryRepository inventories;
ReserveInventoryApplicationService(
OrderRepository orders,
InventoryRepository inventories) {
this.orders = orders;
this.inventories = inventories;
}
void handle(ReserveInventoryCommand command) {
Order order = orders.get(command.orderId());
Inventory inventory = inventories.getBySku(order.sku());
// 业务判断留在领域对象内部,应用层只负责安排调用顺序。
inventory.reserve(order.quantity());
order.markReserved();
inventories.save(inventory);
orders.save(order);
}
}
public static void main(String[] args) {
InMemoryOrderRepository orders = new InMemoryOrderRepository();
InMemoryInventoryRepository inventories = new InMemoryInventoryRepository();
Order order = new Order("O-1001", "SKU-1", 3);
Inventory inventory = new Inventory("SKU-1", 10);
orders.add(order);
inventories.add(inventory);
ReserveInventoryApplicationService service =
new ReserveInventoryApplicationService(orders, inventories);
service.handle(new ReserveInventoryCommand("O-1001"));
System.out.printf(
"订单 %s 状态: %s,剩余库存: %d%n",
order.id(), order.status(), inventory.available());
}
}预期输出
订单 O-1001 状态: RESERVED,剩余库存: 7示例里的应用服务没有直接修改库存字段,也没有自己判断订单状态;这些规则分别由 Inventory.reserve 与 Order.markReserved 维护。这样规则无论被 HTTP 接口、消息消费者还是批处理任务触发,都不会因为入口不同而丢失。
应用层不应该吞掉领域规则
如果应用服务开始出现大量“库存不足就怎样”“订单状态为某值才允许怎样”的条件分支,它就逐渐变成真正的业务模型。结果是同一规则容易在多个用例中重复,领域对象反而只剩属性和 getter/setter。
更稳妥的分工是:应用层决定参与者和调用顺序;实体、值对象、聚合或领域服务决定业务是否成立;Repository 负责取得和保存聚合;基础设施层负责 Repository 的具体存储实现。
把事务边界放在用例周围
真实项目通常会让一次应用服务调用对应一个事务边界,使“扣减库存”和“更新订单状态”要么一起成功,要么一起回滚。事务管理属于用例执行控制,不应迫使领域对象依赖数据库事务 API。
当流程进一步包含支付网关、消息发布等外部系统时,就不能简单假设单个本地事务覆盖所有步骤,需要根据一致性要求采用 Outbox、Saga 等机制。无论实现如何变化,应用服务的核心角色仍是协调一次用例,而领域规则应继续留在最能表达业务语义的领域对象中。