知识卡片

结构图卡 · 1973.e.1.b.1.c.1

Saga处理器只负责消息适配

理解等级:中级

Saga 命令处理器是消息请求的服务端入口,作用类似 REST Controller,但接收的是消息封包并异步回复。生成的 InventoryItemCommandHandler 在指定服务通道上把各封包类型绑定到处理方法;方法拆出原命令后交给 InventoryItemApplicationService,示例中的库存写入处理器再把结果转为成功或失败消息。查询 GetInventoryItemEnvelope 稍有不同:它携带库存 ID,应用服务返回状态后,处理器先转成 InventoryItemStateDto 再回复。这样消息协议适配留在处理器,业务状态变更仍由应用服务承担。InOutCommandHandler 对入库/出库单也使用相同的绑定方式,但其 InternalVoid 示例在异常时直接重抛;这些片段不能证明所有命令都统一生成失败回复。

结构图: 类图只展示书中库存单元示例的关键类型;依赖箭头表示调用或使用,不表示继承。

classDiagram
    class InventoryItemCommandHandler {
        +commandHandlerDefinitions() CommandHandlers
        +getInventoryItem(CommandMessage) Message
        -addInventoryItemEntry(CommandMessage) Message
    }
    class InventoryItemApplicationService {
        +get(InventoryItemId) InventoryItemState
        +when(AddInventoryItemEntry)
    }
    class GetInventoryItemEnvelope {
        +getData() InventoryItemId
    }
    class InventoryItemStateDto
    InventoryItemCommandHandler --> InventoryItemApplicationService : 调用
    InventoryItemCommandHandler ..> GetInventoryItemEnvelope : 接收
    InventoryItemCommandHandler ..> InventoryItemStateDto : 转换并回复
这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    原书出处

    参考来源

    - 位置:《深入实践DDD:以DSL驱动复杂软件开发》第143章《12.3.2 生成的Saga命令处理代码》(源文件:_epub-src/OEBPS/Text/chapter146.xhtml) - 结论依据:原文把 Saga 处理器类比为消息版 REST Controller;库存处理器代码列出通道、封包到方法的绑定,以及写入命令转交应用服务、查询状态转 DTO 后回复的路径。 - 原始内容:为了让服务端能够处理Eventuate Tram Saga命令,需要用到Saga命令处理器(Saga Command Handlers)。 如果你知道如何使用Spring MVC框架(或者其他类似的服务框架)来实现RESTful Services,可以用Spring MVC的REST Controller(@RestController)来类比理解这里所说的“命令处理器”。 - 补充原文:private Message internalVoid(CommandMessage<CommandEnvelopes.InOut.InternalVoidEnvelope> ce) { try { InOutCommands.InternalVoid cmd = ce.getCommand().getData(); inOutApplicationService.when(cmd); return withSuccess(); } catch (Exception e) { throw e; } }