Search code examples
domain-driven-design

What type of data should you choose when you have a one-to-one or many-to-one relationship?


I was looking at Vaughn Vernon's DDD sample repository, and I am completely confused about what decision to make when dealing with one-to-one or many-to-one relationships with other aggregates or non-aggregates, as seen in the Discussion entity/model where one property is represented as an entity and another as an ID (value object). I am a bit lost.

He uses the following properties in the Discussion entity:

https://github.com/VaughnVernon/IDDD_Samples/blob/master/iddd_collaboration/src/main/java/com/saasovation/collaboration/domain/model/forum/Discussion.java

private Author author; // entity
private ForumId forumId; // value object
private Tenant tenant; // entity

Can you help me understand how to correctly choose the appropriate type?

Because if we talk to domain experts a discussion is related to a Forum, not to a forumID, it is something I still find difficult to understand.


Solution

  • It's linked to Aggregate Roots. I haven't seen the code (and read the book many years ago), but for what I see Discussion is a root aggrate, so it should only refer to other Root Aggregates by ID. It looks like Forum is another Root Aggregate.

    This 'barrier' between between Root Aggregates helps to manage complexity. It lowers cognitive load as the bounday of a root aggregate is clear and changes to one Root Aggregate cannot make changes in other Root Aggregates.

    ForumId is considered a value object because it just represents a value. I know that sounds tautological, but bear in mind that most software out there represents ids as long or UUID. Without wrapping it in a more meaningful type. Having a 'long' doesn't make it clear to what aggregate/entity it refers to, or if it's just a random number that represents something else. Using value objects in this way is sometimes called microtypes.