如何在Axon框架中从聚合成员访问聚合根状态

huangapple go评论117阅读模式
英文:

How can I access aggregate root state from aggregate member in axon framework

问题

假设我有一个名为 CloudServer Aggregate 的聚合,其中包含 cloudService 作为其成员。如果用户想要升级他们的 cloudService,我需要计算他们是否有足够的余额来执行此操作,但我不知道如何从 AggregateMember 访问 AggregateRoot 的余额状态。以下是代码的样子。

@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){
    // 我需要检查此云服务器的当前余额,但我也需要使用此类内部的状态
    // if(balance < command.price) {
    //   throw IllegalArgumentException("余额不足")
    // }
    // if(!isActivated){
    //   throw IllegalArgumentException("发生错误")
    // }
  }
}
英文:

Suppose I have CloudServer Aggregate which has cloudService as a member of it. 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. 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&lt;CloudService&gt;()
}

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 &lt; command.price) {
//      throw IllegalArgumentException(&quot;Insufficient balance&quot;)
//    }
//    if(!isActivated){
//      throw IllegalArgumentException(&quot;Some errors&quot;)
//    }
  }
}

答案1

得分: 1

没有直接访问聚合总和的方式。

由于您想要执行的操作取决于聚合根,最好将命令处理程序移动到聚合本身。

检查余额并相应地升级服务。如果余额通过验证,甚至可以直接从聚合根的命令处理程序调用“命令处理程序”函数在服务对象上进行调用。

英文:

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.

答案2

得分: -1

应该有一种方法是通过扩展AnnotatedAggregate来实现的。
然后,您应该能够使用getAggregateRoot()(或在Kotlin中使用aggregateRoot),然后使用'as CloudServerAggregate'进行转换。

英文:

There should be a way by extending AnnotatedAggregate.
Then you should be able to use getAggregateRoot() (or aggregateRoot with kotlin) and then cast with 'as CloudServerAggregate'.

huangapple
  • 本文由 发表于 2020年8月26日 18:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63595532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定