Skip to content

Conversation

@teheran
Copy link

@teheran teheran commented Mar 8, 2023

Comparing two incomparable objects in this way can lead to unpredictable results.

    private int CompareComponents(object object1, object object2)
    {
        ...
        **return object1.Equals(object2) ? 0 : -1;**
    }

Expected if (object1 > object2) then (object2 < object1)

To prevent comparison of non-comparable components of VO it is proposed to change signature of ValueObject.GetEqualityComponents abstract method:

--protected abstract IEnumerable<object> GetEqualityComponents();
++protected abstract IEnumerable<IComparable> GetEqualityComponents();

dmitry korotin added 2 commits March 8, 2023 14:30
Expected if (A > B) then (B < A).
--protected abstract IEnumerable<object> GetEqualityComponents();
++protected abstract IEnumerable<IComparable> GetEqualityComponents();
@teheran teheran changed the title ValueObject comparison symmetry violated Fix symmetry violation of ValueObject comparison Mar 10, 2023
@vkhorikov vkhorikov merged commit 8c73d41 into vkhorikov:master Mar 13, 2023
@vkhorikov
Copy link
Owner

Thanks much. Will publish this shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants