stemmlerjs / ddd-forum

Hacker news-inspired forum app built with TypeScript using DDD practices from solidbook.io.
https://dddforum.com
ISC License
1.93k stars 396 forks source link

IDs as value objects fail equality check #126

Open shreddish opened 4 months ago

shreddish commented 4 months ago

Because value objects use shallow object equality calling equals on the ID value object will always fail. This requires you to access the UniqueEntityID inside the value object prop and calling the equals on that.

CommentId.value.equals(SomeOtherCommentId.value) vs CommentId.equals(SomeOtherCommentId (will always fail)

should we just be adding deep equality instead of shallow equality to the value object equality checks?

timajade1 commented 3 months ago

Deep equality checks are generally preferred for comparing value objects because they ensure that two objects are considered equal if they have the same value, regardless of whether they are different instances. In your case, comparing the internal UniqueEntityID of the value objects for equality seems like the correct approach. This way, you ensure a more accurate comparison based on the underlying data, rather than the object references. Let me know if you find a solution that works better for your use case!