#region ========================================================================= USING ===================================================================================== using System; #endregion namespace Lumina.Domain.Common.Models.Core; /// /// Base class for all domain Entities. /// /// The type representing the unique identifier for the Entity. It should be a non-nullable type. public abstract class Entity : IEquatable> where TId : notnull { #region ==================================================================== PROPERTIES ================================================================================= /// /// Gets the unique identifier of the entity. /// public TId Id { get; protected set; } /// /// Gets the date and time when the entity was created. /// public DateTime Created { get; set; } /// /// Gets the date and time when the entity was last updated. /// public DateTime? Updated { get; set; } #endregion #region ====================================================================== CTOR ===================================================================================== /// /// Initializes a new instance of the class. /// /// The id of the entity. protected Entity(TId id) { Id = id; } #pragma warning disable CS8618 /// /// Initializes a new instance of the class. /// protected Entity() // only needed during reflection { } #pragma warning restore CS8618 #endregion #region ===================================================================== METHODS =================================================================================== /// /// Indicates whether the current object is equal to another object of the same type. /// /// An object to compare with this object. /// if the current object is equal to the other parameter, otherwise. public bool Equals(Entity? other) { return Equals((object?)other); } /// /// Determines whether the specified objects' Id is equal to the current objects' Id. /// /// The object whose Id is compared with the current objects' Id. /// if the specified objects' Id is equal to the current objects' Id, otherwise. public override bool Equals(object? obj) { return obj is Entity entity && Id.Equals(entity.Id); } /// /// Custom implementation of the equality operator. /// /// The left operand of equality. /// The right operand of equality. /// if is equal to , otherwise. public static bool operator ==(Entity left, Entity right) { return Equals(left, right); } /// /// Custom implementation of the inequality operator. /// /// The left operand of equality. /// The right operand of equality. /// if is not equal to , otherwise. public static bool operator !=(Entity left, Entity right) { return !Equals(left, right); } /// /// Override of the default hash function. /// /// A hash code for the current object. public override int GetHashCode() { return Id.GetHashCode(); } #endregion }