我是 DDD 的新手,我想对我在实施它时面临的一些挑战提出一些建议。
我正在使用 Typescript 开发应用程序。数据保存在关系数据库中。我们没有遵循 CQRS 模式,我们的读取和写入发生在同一个数据库中。
假设我有一个User
大致如下所示的聚合,
class User extends AggregateRoot {
id: number;
phone: Phone;
email: Email;
address: Address;
private constructor(id, phone, email, address){
//setting the values
}
public static create(props) {
return new User({...props});
}
public static update(props) {
return new User({...props});
}
}
在这里,Phone
and Email
are ValueObjects
and Address
is an Entity
.
class Phone extends ValueObject {
phNumber: string;
private constructor( ph ) {
phNumber = ph;
}
public static create(ph){
//do validations
return new Phone(ph);
}
}
该类Email
也类似于Phone
.
现在,一旦在控制器中接收到更新电话请求,请求就会被转发到User Service
层,服务将大致如下所示,
public updatePhone( updatePhNoDto ) {
const userEntity = userRepository.getUser(updatePhNoDto.userId);
const userModel = User.update({
id: updatePhNoDto.userId,
phone: Phone.create(userEntity.phone),
email: Email.create(userEntity.email),
address: Address.create(userEntity.address)
});
userRepository.updateUser(userModel)
}
在这里,每次用户请求更新电话号码时,我都会从 RDBMS 中获取用户数据,并对所有已验证的字段进行所有验证,然后调用方法User.update()
。所以,这是我的问题:
- 不确定上述方法是否正确,因为我正在验证我已经验证过的东西,并且可能是不必要的数据库调用。因此,请向我建议处理此类情况的最佳实践,即要求更新单个或仅几个字段。
- 用户可以独立于他的其他信息更新他的地址。那么,
Address
实体应该是独立的Aggregate Root
吗?如果是,如果在单个 http-request 中同时请求更新 UserInfo 和 Address,应该如何处理? - 聚合根在删除中的作用是什么?应该如何在其中建模?
如果您在设计中发现任何其他缺陷,请告诉我。
谢谢!