ddd-forum icon indicating copy to clipboard operation
ddd-forum copied to clipboard

IDs as value objects fail equality check

Open shreddish opened this issue 1 year ago • 1 comments

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?

shreddish avatar Mar 20 '24 12:03 shreddish

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!

timajade1 avatar Apr 15 '24 11:04 timajade1