How to model users and groups in DDD?
除了以下场景是否适合DDD的问题,我想讨论它并寻求建议:
Given we have users and groups. A user has a name, and a group has a name as well. Users may join of leave groups, and they may also change groups. The rules they have to obey are: A user may only be in maximally 2 groups, and a group may consist of maximally 10 users.
你是如何建模的?到目前为止,我可以想到三个选项,每个选项都有其各自的优点和缺点:
用户和组是实体,两者也是聚合体。
用户和组是实体,两者也是聚合体。
用户和组都是实体,都是聚合体。但还有第三个蕴:关系。
谁能给我一个提示?
如果只有两个约束中的一个,那就太容易了:然后你可以把命令放到也有约束的聚合中。但是,如果您对双方都有约束,那我就迷路了。有什么帮助吗?
您可以将一个组的实例交给用户,让它检查包含聚合的不变量。然后让用户知道它加入了群组,并让群组知道有用户加入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Application handle(UserWantsToJoinGroup command) user = users.withId(command.userId) group = groups.withId(command.groupId) user.join(group) class User join(Group g) if g.isFull throw if this.isMemberOf(g) throw if this.numberOfGroupsImIn >= 2 throw publish new JoinedGroup(this.userId, g.groupId) handle(JoinedGroup evt) // modify state class Group handle(JoinedGroup evt) // modifiy state |