[英]How can I access aggregate root state from aggregate member in axon framework
Suppose I have CloudServer Aggregate which has cloudService as a member of it.假设我有 CloudServer Aggregate,它有 cloudService 作为它的成员。 If the users want to upgrade their cloudService I need to calculate if they have enough balance to do so, but I don't know how can I access balance state of AggregateRoot from AggregateMember.
如果用户想要升级他们的 cloudService,我需要计算他们是否有足够的余额来升级,但我不知道如何从 AggregateMember 访问 AggregateRoot 的余额状态。 Here's how the code looks like.
下面是代码的样子。
@Aggregate
class CloudServerAggregate() {
@AggregateIdentifier
lateinit var cloudServerId: String
lateinit var balance: BigDecimal
@AggregateMember(eventForwardingMode = ForwardMatchingInstances::class)
var cloudService = mutableListOf<CloudService>()
}
class CloudService(
@EntityId
val serviceId: String
) {
val isActivated: false
@CommandHandler
fun handle(command: UpgradeServiceCommand){
// I need to check the current balance of this cloud server, but I also need to use the state inside this class
// if(balance < command.price) {
// throw IllegalArgumentException("Insufficient balance")
// }
// if(!isActivated){
// throw IllegalArgumentException("Some errors")
// }
}
}
There is no direct way of accessing aggregate from the aggregate members.没有从聚合成员访问聚合的直接方法。
Since action you want to do is depended from aggregate root, it would be best that you move the command handler to the aggregate itself.由于您想要执行的操作取决于聚合根,因此最好将命令处理程序移动到聚合本身。
Check the balance and upgrade service accordingly.检查余额并相应地升级服务。 You could even call "command handler" function directly on service object from aggregate root's command handler, if balance passes validation.
如果余额通过验证,您甚至可以从聚合根的命令处理程序直接在服务对象上调用“命令处理程序”函数。
There should be a way by extending AnnotatedAggregate.应该有一种扩展 AnnotatedAggregate 的方法。 Then you should be able to use getAggregateRoot() (or aggregateRoot with kotlin) and then cast with 'as CloudServerAggregate'.
然后您应该能够使用 getAggregateRoot()(或使用 kotlin 的aggregateRoot),然后使用“as CloudServerAggregate”进行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.