Skip to content

Commit

Permalink
#237 ValueObject : IComparable<ValueObject> is Potentially Breaking d…
Browse files Browse the repository at this point in the history
…ue to Type Ambiguity
  • Loading branch information
vkhorikov committed Nov 3, 2020
1 parent 70a9757 commit 98dc557
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,14 @@ public void Can_sort_value_objects_containing_nulls()
}

[Fact]
public void Sorting_value_objects_returns_same_collection_if_one_of_properties_doesnt_implement_IComparable()
public void Two_value_objects_are_not_equal_if_they_contain_non_comparable_components_of_different_values()
{
var vo1 = new VOWithObjectProperty(new object());
var vo2 = new VOWithObjectProperty(new object());

VOWithObjectProperty[] array = new[] { vo1, vo2 }
.OrderBy(x => x)
.ToArray();
int result = vo1.CompareTo(vo2);

array[0].Should().Be(vo1);
array[1].Should().Be(vo2);
result.Should().NotBe(0);
}

private class VOWithObjectProperty : ValueObject
Expand Down
9 changes: 3 additions & 6 deletions CSharpFunctionalExtensions/ValueObject/ValueObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,10 @@ private int CompareComponents(object object1, object object2)
if (object2 is null)
return 1;

var component1 = object1 as IComparable;
var component2 = object2 as IComparable;
if (object1 is IComparable comparable1 && object2 is IComparable comparable2)
return comparable1.CompareTo(comparable2);

if (component1 == null || component2 == null)
return 0;

return component1.CompareTo(component2);
return object1.Equals(object2) ? 0 : -1;
}

public int CompareTo(ValueObject other)
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2.12.1
2.12.2
New features:
* None

Expand Down

0 comments on commit 98dc557

Please sign in to comment.