我有下面的聚合,它包含一个聚合成员。
@Aggregate
public class CpaAggregate {
@AggregateIdentifier
private String externalId;
@AggregateMember
private Entity entity;
public CpaAggregate() {
}
@CommandHandler
public CpaAggregate(CreateCpaCommand cmd) {
AggregateLifecycle.apply(new CpaCreatedEvent(...));
}
@EventSourcingHandler
protected void on(CpaCreatedEvent evt) {
....
}
}
public class Entity {
@EntityId
private String entityId;
private Set<Identifier> identifiers = new HashSet<>();
public Entity() {
}
@EventSourcingHandler
public void on(IdentifiantUpdatedEvent evt) {
...
}
}
public class Laboratory extends Entity {
private OperatingSystem operatingSystem;
public Laboratory() {
}
@CommandHandler
public void handle(UpdateIdentifierLABCommand cmd) {
AggregateLifecycle.apply(new IdentifiantUpdatedEvent(....));
}
}
commandGateway.sendAndWait(new UpdateIdentifierLABCommand(...));
当我发送命令更新实验室类型实体的标识符时,我得到了这个错误
org.axonframework.commandhandling.NoHandlerForCommandException:否处理程序已订阅命令[UpdateIdentifierLABCommand]
CpaAggregate
进行稍微不同的建模。
我不使用一般的Entity
聚合成员,而是使用更具体的实体,如Laboratory
实例。
首先,随着模型结构变得更加清晰,这在建模方面要清晰得多。其次,AxonFramework将进入父类以获取细节。因此,在Entity
类中仍然可以有公共信息,如实体标识符、命令处理程序和事件源处理程序。
因此,我会这样调整:
@Aggregate
public class CpaAggregate {
@AggregateIdentifier
private String externalId;
@AggregateMember
private Laboratory laboratory;
public CpaAggregate() {
}
@CommandHandler
public CpaAggregate(CreateCpaCommand cmd) {
AggregateLifecycle.apply(new CpaCreatedEvent(...));
}
@EventSourcingHandler
protected void on(CpaCreatedEvent evt) {
....
}
}
顺便说一句,这应该确保AxonFramework也能在聚合成员中发现命令处理程序。