简体   繁体   English

如何从轴突框架中的聚合成员访问聚合根状态

[英]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.

相关问题 轴突再造骨料 state 不清楚 - Axon recreating aggregate state not clear Axon 框架:如何在单元测试中检索状态存储的聚合 id 的 id - Axon framework: How to retrieve id of a state-stored aggregate id in a unit test Axon:事件源聚合无状态更改事件 - Axon: Eventsourced Aggregate none state changing event Axon框架-对每个聚合根使用单独的Mongo域事件集合 - Axon framework - using separate Mongo collection of domain events for each aggregate root 聚合中的实体可以移动到 Axon 中的另一个聚合(相同类型)吗 - can an entity within an aggregate move to another aggregate (same type) in Axon 在Axon中重命名聚合后如何“转换”聚合类型 - How to “upcast” the aggregate type after an aggregate was renamed in Axon 如何在轴心框架的Java中不使用Spring的情况下配置处理命令和调度事件的聚合类? - How to configure aggregate class which handles command and dispatch event without using spring in java with axon framework? Axon 框架:聚合自动装配 Bean 在测试中抛出 NullPointerException - Axon Framework: Aggregate Autowired Bean throws NullPointerException in Test 聚合根与复合根有何不同? - How is an aggregate root different from an composite? 轴突聚合标识符类型转换器 - Axon Aggregate Identifier Type Converter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM