Skip to content

Commit

Permalink
Fix equality of typed EntityIds
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Jan 22, 2024
1 parent dce246a commit 466bf5d
Showing 1 changed file with 33 additions and 61 deletions.
94 changes: 33 additions & 61 deletions src/NexusMods.EventSourcing.Abstractions/EntityId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,11 @@ public int CompareTo(EntityId other)
}
}

/// <summary>
/// Interface for a strongly typed <see cref="EntityId"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IEntityId<out T> where T : IEntity
{

}

/// <summary>
/// A typed <see cref="EntityId"/> for a specific <see cref="IEntity"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
public readonly struct EntityId<T> : IEntityId<T> where T : IEntity
public readonly struct EntityId<T> : IEquatable<EntityId<T>>, IComparable<EntityId<T>> where T : IEntity
{
/// <summary>
/// Creates a new instance of <see cref="EntityId{T}"/>.
Expand Down Expand Up @@ -198,69 +189,50 @@ public static EntityId<T> From(ReadOnlySpan<char> id)
}
return From(UInt128.Parse(id, NumberStyles.HexNumber));
}
}


/*
/// <inheritdoc />
public bool Equals(EntityId<T> other)
{
return Id.Equals(other.Id);
}

/// <summary>
/// A strongly typed <see cref="EntityId"/> for a specific <see cref="IEntity"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
public readonly struct EntityId<in T> where T : IEntity
{
/// <summary>
/// Creates a new instance of <see cref="EntityId{T}"/>.
/// </summary>
/// <returns></returns>
public static EntityId<T> NewId() => new(EntityId.NewId());
/// <inheritdoc />
public int CompareTo(EntityId<T> other)
{
return Id.CompareTo(other.Id);
}

/// <inheritdoc />
public override bool Equals(object? obj)
{
return obj is EntityId<T> other && Equals(other);
}

/// <summary>
/// Gets the <see cref="EntityId{T}"/> from the specified <paramref name="id"/>.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static EntityId<T> From(UInt128 id) => new(EntityId.From(id));
/// <inheritdoc />
public override int GetHashCode()
{
return Id.GetHashCode();
}

/// <summary>
/// Reads the <see cref="EntityId{T}"/> from the specified <paramref name="data"/>.
/// Compares two <see cref="EntityId{T}"/>s for equality.
/// </summary>
/// <param name="data"></param>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static EntityId<T> From(ReadOnlySpan<byte> data) => new(EntityId.From(data));
public static bool operator ==(EntityId<T> left, EntityId<T> right)
{
return left.Equals(right);
}

/// <summary>
/// Converts the <see cref="EntityId{T}"/> to a <see cref="EntityId"/>.
/// Compares two <see cref="EntityId{T}"/>s for inequality.
/// </summary>
/// <param name="id"></param>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static implicit operator EntityId(EntityId<T> id) => id.Value;
/// <summary>
/// Creates a new instance of <see cref="EntityId{T}"/>.
/// </summary>
/// <param name="id"></param>
public EntityId(EntityId id) => Value = id;
/// <summary>
/// The underlying value.
/// </summary>
public readonly EntityId Value;
/// <inheritdoc />
public override string ToString()
public static bool operator !=(EntityId<T> left, EntityId<T> right)
{
return typeof(T).Name + "<" + Value.Value.ToString("X") + ">";
return !(left == right);
}
}
*/

0 comments on commit 466bf5d

Please sign in to comment.