DDD 建模问题:学生、教室、座位和学生最喜欢的座位

发布于 2024-08-26 16:37:21 字数 189 浏览 7 评论 0原文

我不知道如何模拟这种关系......

教室里有很多座位,每个学生都在教室里学习,并且在里面有一个最喜欢的座位。

在我看来,我有两个聚合根:教室和学生,座位 a 是由教室聚合的实体...

并且对于学生来说,拥有最喜欢的座位,它必须持有对其的引用(座位不是聚合)根)。

有什么建议吗? 预先感谢,埃里克。

i'm not sure how to model this relationship...

A classroom contains many seats, every student studies in a classroom and have a favorite seat within it.

The way i see it, i have two aggregate roots: classroom and student, seats a are entities aggregatged by classroom...

And for a student to have a fovorite seat, it must hold a reference to it (seat isn't an aggregate root).

Any suggestions?
Thanks in advance, Erik.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夢归不見 2024-09-02 16:37:21

嗯,这要看情况...我的教室里可能会有像你这样的座位。学生可以是实体,但座位对我来说更像是有价值的物体。然而,这一切都取决于您的背景。我不确定什么对你来说重要,如果我的建议没有意义,请解释你的背景。

Hmm, it depends... I'd maybe have classroom aggregate with seats like you. Students could be entities, but seats look more like value objects to me. However, all that depends on your context. I am not sure what is important for you, if my suggestion doesn't make sense, please explain your context.

墨落成白 2024-09-02 16:37:21

一个可能的选择是为每个座位都有一个最喜欢该座位的学生列表。课堂聚合根将负责维持这样的约束:学生在教室中不能拥有多个最喜欢的座位。使用此选项,座位是一个实体(不是值对象),由教室聚合并引用学生。

A possible option would for each seat have a list of the students that the seat is favorite of. The Classroom aggregate root would be responsible for maintaining the constraint that a student can not have more than one favorite seat in the classroom. With this option seat is an entity (not a value object) aggregated by classroom with references to the students.

温柔一刀 2024-09-02 16:37:21

听起来您需要引入一个值对象 Location,它描述教室中的一个位置,然后您可以在学生班级中对它们进行建模:

public IDictionary<Classroom, Location> FavoriteSeats;

教室可以引用 Seat 实体的位置。

(如果它们确实首先的实体 - 您甚至可能会发现您不需要将座椅建模为实体(例如,“闻起来像汉堡包的蓝色塑料椅子)。)

It sounds like you need to introduce a value object, Location, which describes a spot in a classroom, and you can then model them like this in the student class:

public IDictionary<Classroom, Location> FavoriteSeats;

Classrooms can reference Seat entities by their locations.

(If they really are entities in the first place - you may even discover that you don't need to model seats as entities (e.g., "The blue plastic chair that smells like hamburger).)

巡山小妖精 2024-09-02 16:37:21

OP 几个月后,但以防万一它有用。

@Jeff Sternal 走在正确的轨道上。该问题从根本上讲是关于管理学生、教室和座位之间的三元(三元)关系。由于这是一个关系问题,所以我们暂时保留关系概念并将其称为 FavouriteSeat(Student, Classroom, Seat)。

如上所述,除了唯一标识符之外,该问题不需要任何关于它们的知识。现在让我们使用伪类型“ID”(并不重要,只要每个都是唯一的)。

在伪 SQL 中,我们有:

create table FavouriteSeat(
   StudentID ID not null,
   ClassroomID ID not null,
   SeatID ID not null)

该表上的标准 CRUD(创建/读取/更新/删除)操作提供了所需的所有操作。然而,它缺少一些限制。现在,学生在教室里可以有 2 个或更多最喜欢的座位。尽管没有明确说明,OP 暗示学生在任何给定的教室中最多应该有一个(0 或 1)个最喜欢的座位。

我们可以通过在表上添加唯一键来解决这个问题。再次使用伪 SQL:

alter table FavouriteSeat add unique key (StudentID, ClassroomID) 

现在,对于任何给定的教室,学生只能拥有一个最喜欢的座位。还有一个问题。该解决方案目前不支持定义哪些教室有哪些可用座位。我们可以通过以下方法解决这个问题:

  1. 添加一个新表,列出每个教室的有效座位:

    创建表 ClassroomSeat (
    ClassroomID ID 不为空,
    SeatID ID 不为空)

    alter table ClassroomSeat add unique key (ClassroomID, SeatID)

  2. 向 FavouriteSeat 表添加外键,以便它只能引用有效的 ClassroomSeats:

    alter table FavouriteSeat addforeign Key FK_Seat (ClassroomID, SeatIDreferences FavouriteSeat (ClassroomID, SeatID).

就是这样,

关系模型可以很容易地在 OO/DDD 中进行转换。 ClassroomSeat 具有用于 CRUD 操作的方法,这些方法必须强制执行上述的唯一和外键约束,

Customer、Classroom 和 Seat 可以是值类型(尽管可能存在更广泛的未声明的要求)。无论哪种方式,他们都需要一个可以在 FavouriteSeat 方法中检查的唯一标识符属性。

Months after the OP, but just in case it's useful.

@Jeff Sternal is on the right track. The problem is fundamentally about managing a 3 way (ternary) relation among students, classrooms and seats. Since it's a relational problem, let's stay with relational concepts for now and call it FavouriteSeat(Student, Classroom, Seat).

As described, the problem doesn't require any knowledge about any of them other than a unique identifier. Let's use a pseudo type 'ID' for now (doesn't really matter, as long as each is unique).

In pseudo-sql, we have:

create table FavouriteSeat(
   StudentID ID not null,
   ClassroomID ID not null,
   SeatID ID not null)

Standard CRUD (create/read/update/delete) ops on that table provide all the operations required. However, it's missing some constraints. Right now, a student could have 2 or more favourite seats in a classroom. Although not explicitly stated, the OP implies that a student should have at most one (0 or 1) favourite seat in any given classroom.

We can fix that by adding a unique key on the table. Again using pseudo-sql:

alter table FavouriteSeat add unique key (StudentID, ClassroomID) 

Now a student can only have one favourite seat for any given classroom. There's still a remaining question. The solution doesn't currently support defining what seats are available in what classrooms. We can solve that by:

  1. Adding a new table that lists the valid seats in each classroom:

    create table ClassroomSeat (
    ClassroomID ID not null,
    SeatID ID not null)

    alter table ClassroomSeat add unique key (ClassroomID, SeatID)

  2. Adding a foreign key to the FavouriteSeat table so it can only reference valid ClassroomSeats:

    alter table FavouriteSeat add Foreign Key FK_Seat (ClassroomID, SeatID references FavouriteSeat (ClassroomID, SeatID).

That's it. 2 relations, 3 keys and standard CRUD operations cover all the requirements as stated.

The relational model can be translated in OO/DDD pretty easily. It needs an Entity for FavouriteSeat and ClassroomSeat with methods for the CRUD ops. The methods would have to enforce the unique & foreign key constraints above.

To meet just the requirements stated, Customer, Classroom and Seat could be value types (although there may be wider unstated reqs that may change that). Either way they need a unique identifier property that can be checked in the FavouriteSeat methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文