From 466bf5da67e67c0ed3c5eaa336d2287dd3e5c66e Mon Sep 17 00:00:00 2001 From: halgari Date: Mon, 22 Jan 2024 15:52:16 -0700 Subject: [PATCH] Fix equality of typed EntityIds --- .../EntityId.cs | 94 +++++++------------ 1 file changed, 33 insertions(+), 61 deletions(-) diff --git a/src/NexusMods.EventSourcing.Abstractions/EntityId.cs b/src/NexusMods.EventSourcing.Abstractions/EntityId.cs index 9ee1670b..b233e213 100644 --- a/src/NexusMods.EventSourcing.Abstractions/EntityId.cs +++ b/src/NexusMods.EventSourcing.Abstractions/EntityId.cs @@ -119,20 +119,11 @@ public int CompareTo(EntityId other) } } -/// -/// Interface for a strongly typed . -/// -/// -public interface IEntityId where T : IEntity -{ - -} - /// /// A typed for a specific . /// /// -public readonly struct EntityId : IEntityId where T : IEntity +public readonly struct EntityId : IEquatable>, IComparable> where T : IEntity { /// /// Creates a new instance of . @@ -198,69 +189,50 @@ public static EntityId From(ReadOnlySpan id) } return From(UInt128.Parse(id, NumberStyles.HexNumber)); } -} - -/* + /// + public bool Equals(EntityId other) + { + return Id.Equals(other.Id); + } -/// -/// A strongly typed for a specific . -/// -/// -public readonly struct EntityId where T : IEntity -{ - /// - /// Creates a new instance of . - /// - /// - public static EntityId NewId() => new(EntityId.NewId()); + /// + public int CompareTo(EntityId other) + { + return Id.CompareTo(other.Id); + } + /// + public override bool Equals(object? obj) + { + return obj is EntityId other && Equals(other); + } - /// - /// Gets the from the specified . - /// - /// - /// - public static EntityId From(UInt128 id) => new(EntityId.From(id)); + /// + public override int GetHashCode() + { + return Id.GetHashCode(); + } /// - /// Reads the from the specified . + /// Compares two s for equality. /// - /// + /// + /// /// - public static EntityId From(ReadOnlySpan data) => new(EntityId.From(data)); - + public static bool operator ==(EntityId left, EntityId right) + { + return left.Equals(right); + } /// - /// Converts the to a . + /// Compares two s for inequality. /// - /// + /// + /// /// - public static implicit operator EntityId(EntityId id) => id.Value; - - - - - - /// - /// Creates a new instance of . - /// - /// - public EntityId(EntityId id) => Value = id; - - /// - /// The underlying value. - /// - public readonly EntityId Value; - - /// - public override string ToString() + public static bool operator !=(EntityId left, EntityId right) { - return typeof(T).Name + "<" + Value.Value.ToString("X") + ">"; + return !(left == right); } - - - } - -*/