Skip to content

Commit

Permalink
Implemented the comparable contract for the primitive value object. (#14
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mgernand authored May 28, 2022
1 parent 6f377be commit df6ad76
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/Fluxera.ValueObject/PrimitiveValueObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
/// <typeparam name="TValueObject">The type of the value object.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
[PublicAPI]
public abstract class PrimitiveValueObject<TValueObject, TValue> : ValueObject<TValueObject>
where TValueObject : ValueObject<TValueObject>
public abstract class PrimitiveValueObject<TValueObject, TValue> : ValueObject<TValueObject>, IComparable<PrimitiveValueObject<TValueObject, TValue>>
where TValueObject : PrimitiveValueObject<TValueObject, TValue>
where TValue : IComparable
{
static PrimitiveValueObject()
{
Expand All @@ -43,6 +44,19 @@ protected PrimitiveValueObject(TValue value)
/// </summary>
public TValue Value { get; private set; }


/// <inheritdoc />
public int CompareTo(PrimitiveValueObject<TValueObject, TValue> other)
{
return (this.Value, other.Value) switch
{
(null, null) => 0,
(null, _) => -1,
(_, null) => 1,
(_, _) => this.Value.CompareTo(other.Value)
};
}

/// <inheritdoc />
protected sealed override IEnumerable<object> GetEqualityComponents()
{
Expand Down
11 changes: 9 additions & 2 deletions src/Fluxera.ValueObject/ValueObject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Fluxera.ValueObject
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -10,7 +11,7 @@
/// </summary>
/// <typeparam name="TValueObject">The type of the value object.</typeparam>
[PublicAPI]
public abstract class ValueObject<TValueObject>
public abstract class ValueObject<TValueObject> : IEquatable<TValueObject>
where TValueObject : ValueObject<TValueObject>
{
/// <summary>
Expand All @@ -22,6 +23,12 @@ public abstract class ValueObject<TValueObject>
/// </remarks>
private const int HashMultiplier = 37;

/// <inheritdoc />
public bool Equals(TValueObject other)
{
return this.Equals(other as object);
}

/// <summary>
/// Checks if the given value objects are equal.
/// </summary>
Expand Down Expand Up @@ -69,7 +76,7 @@ public sealed override int GetHashCode()
{
// It is possible for two objects to return the same hash code based on
// identically valued properties, even if they are of different types,
// so we include the value object type in the hash calculation
// so we include the value object type in the hash calculation.
int hashCode = this.GetType().GetHashCode();

foreach(object component in this.GetEqualityComponents())
Expand Down
12 changes: 9 additions & 3 deletions tests/Fluxera.ValueObject.UnitTests/Model/Address.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Fluxera.ValueObject.UnitTests.Model
{
using System.Threading.Channels;
using Guards;
using System;
using Fluxera.Guards;
using JetBrains.Annotations;

[PublicAPI]
public class Address : ValueObject<Address>
public class Address : ValueObject<Address>, IComparable
{
public Address(string street, string houseNumber, string postCode, string city)
{
Expand All @@ -27,5 +27,11 @@ public Address(string street, string houseNumber, string postCode, string city)
public string PostCode { get; }

public string City { get; }

/// <inheritdoc />
public int CompareTo(object? obj)
{
return string.Compare(this.PostCode, obj as string, StringComparison.Ordinal);
}
}
}

0 comments on commit df6ad76

Please sign in to comment.