在 Spine 事件引擎的聚合中应用拒绝

Applying rejections in Aggregates in Spine Event Engine

我正在尝试对抛出它的聚合应用拒绝:

public class WalletAggregate extends Aggregate<WalletId, Wallet, Wallet.Builder> {

    @Assign MoneyCharged handle(ChargeMoney cmd) throws InsuffisientFunds {
        ...
    }

    @Apply void on(MoneyCharged event) {
        ...
    }

    @Apply void on(InsuffisientFunds event) {
        ...
    }
}

我可以清楚地看到应用了 MoneyChanged 事件。但是当抛出 InsuffisientFunds 时,它不会被应用。我错过了什么吗?如何将抛出的拒绝应用于聚合?

拒绝不是这样的。拒绝不仅仅是消极事件。当尝试进行非法操作并且处理程序(在您的情况下为聚合)拒绝(拒绝)执行命令时,它会被发出,因此不会更改其状态。

实际上,您可能需要考虑创建一个单独的实体来处理拒绝。 如果您想存储用户的失败操作,Projection 可能效果最好:

public class ChargeAttemptsProjection extends Projection<...> {

    @Subscribe
    void on(InsuffisientFunds rejection) {
        // Update the state of the Projection.
    }
}

如果有故障恢复的流程,ProcessManager听起来更合适。

public class FailedTransactionRecovery extends ProcessManager<...> {

    @React
    YourOtherEvent on(InsuffisientFunds rejection) {
        // Start the recovery process.
    }
}

最后但并非最不重要的一点是,始终可以在客户端订阅拒绝以在 UI 上优雅地处理它。 有关客户端订阅的更多信息,请参阅 the JS reference documentation