我是Axon的新手,也许我错过了一些东西,但需要帮助才能理解。
我有一个简单的食品车聚合。
以下是示例:
@Aggregate
class FoodCard {
@AggregateIdentifier
private lateinit var foodCardId: UUID
private lateinit var selectedProduct: MutableMap<UUID, Int>
constructor()
@CommandHandler
constructor(command: CreateFoodCartCommand) {
AggregateLifecycle.apply(FoodCartCreateEvent(
UUID.randomUUID()
))
}
@CommandHandler
fun handle(command: SelectProductCommand) {
AggregateLifecycle
.apply(ProductSelectedEvent(foodCardId, command.productId, command.quantity))
}
@CommandHandler
fun handle(command: DeleteFoodCartCommand) {
AggregateLifecycle
.apply(FoodCartDeleteEvent(foodCardId))
}
@CommandHandler
fun handle(command: DeselectProductCommand) {
val productId = command.productId
if (!selectedProduct.containsKey(productId)) {
throw ProductDeselectionException("ProductDeselectionException")
}
AggregateLifecycle
.apply(ProductDeselectEvent(foodCardId, productId, command.quantity))
}
@EventSourcingHandler
fun on(event: FoodCartCreateEvent) {
foodCardId = event.foodCardId
selectedProduct = mutableMapOf()
}
@EventSourcingHandler
fun on(event: ProductSelectedEvent) {
selectedProduct.merge(
event.productId,
event.quantity
) {a, b -> a + b}
}
}
作为ES,我正在使用Axon服务器。对于FoodCard投影仪,我使用的是连接到DB的JPA存储库。
我想获得所有包含特殊产品(具体UUID(的食品卡,并将所有食品的数量更改为-1。
我知道有两种类型的动作>读写
那么问题是如何用Axon正确地实现这个流程呢?
感谢
根据您的解释和代码,我认为您可能需要完成DeselectProductCommand
的实现,为ProductDeselectEvent
引入EventSourcingHandler
。如果我正确理解你的";数量;信息存储在selectProduct映射中。在这种情况下,根据您的代码,我看到应该减去产品的数量信息在命令中。
您还需要一个查询,如FindAllFoodCardByProductId
,它将检索包含某个productId的foodCardIdaggregate标识符:此操作将通过jpa存储库在您的Projection上执行。作为参考,您可以在此处查看参考指南https://docs.axoniq.io/reference-guide/implementing-domain-logic/query-handling关于如何在控制器中使用QueryGateway
并在投影中实现QueryHandler
。科拉多。