Adds the base class of all metadata objects

This commit is contained in:
BinaryAlley committed 2024-08-18 14:18:04 +03:00
1 parent 1667036009
commit 1e902e47dc
6 files changed
+109 -4

No files matched your search

@@ -0,0 +1,83 @@
namespace Lumina.Domain.Common.Models.Core;
/// <summary>
/// Base class for all domain Entities.
/// </summary>
/// <typeparam name="TId">The type representing the unique identifier for the Entity. It should be a non-nullable type.</typeparam>
public abstract class Entity<TId> : IEquatable<Entity<TId>> where TId : notnull
{
#region ==================================================================== PROPERTIES =================================================================================
public TId Id { get; protected set; }
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Overload C-tor.
/// </summary>
/// <param name="id">The id of the entity.</param>
protected Entity(TId id)
{
Id = id;
}
#pragma warning disable CS8618
protected Entity() // only needed during reflection
{
}
#pragma warning restore CS8618
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns><see langword="true"/> if the current object is equal to the other parameter, <see langword="false"/> otherwise.</returns>
public bool Equals(Entity<TId>? other)
{
return Equals((object?)other);
}
/// <summary>
/// Determines whether the specified objects' Id is equal to the current objects' Id.
/// </summary>
/// <param name="obj">The object whose Id is compared with the current objects' Id.</param>
/// <returns><see langword="true"/> if the specified objects' Id is equal to the current objects' Id, <see langword="false"/> otherwise.</returns>
public override bool Equals(object? obj)
{
return obj is Entity<TId> entity && Id.Equals(entity.Id);
}
/// <summary>
/// Custom implementation of the equality operator.
/// </summary>
/// <param name="left">The left operand of equality.</param>
/// <param name="right">The right operand of equality.</param>
/// <returns><see langword="true"/> if <paramref name="left"/> is equal to <paramref name="right"/>, <see langword="false"/> otherwise.</returns>
public static bool operator ==(Entity<TId> left, Entity<TId> right)
{
return Equals(left, right);
}
/// <summary>
/// Custom implementation of the inequality operator.
/// </summary>
/// <param name="left">The left operand of equality.</param>
/// <param name="right">The right operand of equality.</param>
/// <returns><see langword="true"/> if <paramref name="left"/> is not equal to <paramref name="right"/>, <see langword="false"/> otherwise.</returns>
public static bool operator !=(Entity<TId> left, Entity<TId> right)
{
return !Equals(left, right);
}
/// <summary>
/// Override of the default hash function.
/// </summary>
/// <returns>A hash code for the current object.</returns>
public override int GetHashCode()
{
return Id.GetHashCode();
}
#endregion
}