Axon 参考指南
  • 介绍
  • 架构概览
    • DDD & CQRS 概念
    • 事件溯源
    • 事件驱动的微服务
  • Axon Server
  • 发行说明
    • Axon Framework
      • Major Releases
      • Minor Releases
    • Axon Server
      • Major Releases
      • Minor Releases Standard Edition
      • Minor Releases Enterprise Edition
    • Axon Framework Extensions
      • AMQP
        • Major Releases
      • CDI
        • Major Releases
      • JGroups
        • Major Releases
      • Kafka
        • Major Releases
        • Minor Releases
      • Kotlin
        • Experimental Releases
      • Mongo
        • Major Releases
        • Minor Releases
      • Reactor
        • Major Releases
        • Minor Releases
      • Spring Cloud
        • Major Releases
        • Minor Releases
      • Tracing
        • Major Releases
        • Minor Releases
  • Getting Started
    • 快速开始
  • Axon Framework
    • 介绍
    • 消息传递概念
      • 消息剖析
      • 消息关联
      • 消息拦截
      • 支持带注解的处理程序
      • 异常处理
      • 工作单元
    • 命令
      • 建模
        • 聚合
        • 多实体聚合
        • 聚合状态存储
        • 从另一个聚合创建聚合
        • 聚合多态性
        • 解决冲突
      • 命令调度器
      • 命令处理程序
      • 基础设施
      • 配置
    • 事件
      • 事件调度器
      • 事件处理程序
      • 事件处理器
        • 订阅事件处理器
        • 流式事件处理器
      • 事件总线和事件存储
      • 事件版本控制
    • 查询
      • 查询处理
      • 查询调度器
      • 查询处理程序
      • 实现
      • 配置
    • 长时处理过程(Sagas)
      • 实现
      • 关联
      • 基础设施
    • Deadlines
      • Deadline Managers
      • Event Schedulers
    • 测试
      • 命令 / 事件
      • 长时处理过程(Sagas)
    • 序列化
    • 调整
      • 事件快照
      • 事件处理
      • 命令处理
    • 监控和指标
    • Spring Boot 集成
    • 模块
  • Axon Server
    • 介绍
    • 安装
      • 本地安装
        • Axon Server SE
        • Axon Server EE
      • Docker / K8s
        • Axon Server SE
        • Axon Server EE
    • 管理
      • 配置
        • System Properties
        • Command Line Interface
        • REST API
        • GRPC API
      • Monitoring
        • Actuator Endpoints
        • gRPC Metrics
        • Heartbeat Monitoring
      • Clusters
      • Replication Groups
      • Multi-Context
      • Tagging
      • Backup and Messaging-only Nodes
      • Backups
      • Recovery
      • Plugins
      • Error Codes
    • 安全
      • SSL
      • 访问控制
      • 访问控制 - 标准版
      • 访问控制 - 企业版
      • 访问控制 - 客户端应用程序
      • 访问控制 - 命令行
      • 访问控制 - REST API
      • 访问控制 - LDAP
      • 访问控制 - OAuth 2.0
    • 性能
      • 事件段
      • 流量控制
    • 迁移
      • Standard to Enterprise Edition
      • Non-Axon Server to Axon Server
  • Extensions
    • Spring AMQP
    • JGroups
    • Kafka
    • Kotlin
    • Mongo
    • Reactor
      • Reactor Gateways
    • Spring Cloud
    • Tracing
  • Appendices
    • A. RDBMS Tuning
    • B. Message Handler Tuning
      • 参数解析器
      • 处理程序增强
    • C. 元数据注解
    • D. 标识符生成
    • E. Axon Server Query Language
由 GitBook 提供支持
在本页
  1. Axon Framework
  2. 命令
  3. 建模

从另一个聚合创建聚合

Aggregate Creation from another Aggregate

上一页聚合状态存储下一页聚合多态性

最后更新于2年前

Regularly, instantiating a new Aggregate is done by issuing a creation command which is handled by a @CommandHandler annotated Aggregate constructor. Such commands could for example be published by a simple or an as a reaction to a certain event. Sometimes the Domain however describes certain Entities to be created from another Entity. In this scenario it would thus be more faithful to the domain to instantiate an Aggregate from it's parent Aggregate.

Aggregate-from-Aggregate Use Case

The most suitable scenario to create a "child" Aggregate from a "parent" Aggregate, is when the decision to create the child lies within the context of a parent Aggregate. This can for example manifest itself if the parent Aggregate contains the necessary state which can drive this child-creation decision.

How to create an Aggregate from another Aggregate

Let us assume we have a ParentAggregate, that upon handling a certain command will decide to create an ChildAggregate. To achieve this, ParentAggregate would look something like this:

import org.axonframework.commandhandling.CommandHandler;

import static org.axonframework.modelling.command.AggregateLifecycle.createNew;

public class ParentAggregate {

    @CommandHandler
    public void handle(SomeParentCommand command) {
        createNew(
            ChildAggregate.class, 
            () -> new ChildAggregate(/* provide required constructor parameters if applicable */)
        ); 
    }
    // omitted no-op constructor, event sourcing handlers and other command handlers
}

The AggregateLifecycle#createNew(Class<T>, Callable<T>) is key to instantiation another Aggregate like our ChildAggregate as a reaction to handling a command. The first parameter to the createNew method is the Class of the Aggregate to be created. The second parameter is the factory method, which expects the outcome to be an object identical to the given type.

The ChildAggregate implementation would in this scenario resemble the following format:

import static org.axonframework.modelling.command.AggregateLifecycle.apply;

public class ChildAggregate {

    public ChildAggregate(String aggregateId) {
        apply(new ChildAggregateCreatedEvent(aggregateId));
    }
    // omitted no-op constructor, command and event sourcing handlers
}

Note that a ChildAggregateCreatedEvent is explicitly applied to notify the ChildAggregate was created, as this knowledge would otherwise be enclosed in the SomeParentCommand command handler of the ParentAggregate.

Creating Aggregates from Event Sourcing Handlers?

Creation of a new Aggregate should be done in a command handler rather than in an event sourcing handler. The rationale behind this is that you do not want to create new child Aggregates when a parent Aggregate is sourced from its events, as this would undesirably create new child Aggregate instances

If the createNew method is however accidentally called within an event sourcing handler, an UnsupportedOperationException will be thrown as stop gap solution.

REST endpoint
Event Handling Component