【发布时间】:2019-05-09 12:35:35
【问题描述】:
我们可以在命令中使用值对象吗?
假设我有一个商店(聚合),其中有一个值对象地址。 在值对象构造函数 Address 中,我放置了一些地址验证逻辑。 因此,如果我在命令 (CreateShopCmd) 中使用该地址对象,那么它会在创建命令时得到验证,但我想要或读取的验证应该存在于命令处理程序中。
但问题是,我必须再次将该验证放在命令处理程序中(因为验证已经存在于它的地址构造函数中),如果我没有将它放在命令处理程序中,那么验证将在我制作时发生事件处理程序中的地址对象并分配给商店聚合(不正确)
所以,请指导我。
以下是代码示例
@Aggregate
@AggregateRoot
public class Shop {
@AggregateIdentifier
private ShopId shopId;
private String shopName;
private Address address;
@CommandHandler
public Shop(CreateShopCmd cmd){
//Validation Logic here , if not using the Address in
// in cmd
//Fire an event after validation
ShopRegistredEvt shopRegistredEvt = new ShopRegistredEvt();
AggregateLifecycle.apply(shopRegistredEvt);
}
@EventSourcingHandler
public void on(ShopRegistredEvt evt) {
this.shopName = evt.getShopName();
//Validation happend here if not put in cmd at the time of making
//Address object - this is wrong
this.address = new Address(evt.getCity(),evt.getCountry(),evt.getZipCode())
}
}
public class CreateShopCmd{
private String shopId;
private String shopName;
private String city;
private String zipCode;
private String country;
}
public ShopCreatedEvent{
private String shopId;
private String shopName;
private String city;
private String zipCode;
private String country;
}
【问题讨论】:
标签: domain-driven-design cqrs event-sourcing axon