diff --git a/src/Lumina.Domain/Common/Models/AggregateRoot.cs b/src/Lumina.Domain/Common/Models/AggregateRoot.cs new file mode 100644 index 00000000..053419a5 --- /dev/null +++ b/src/Lumina.Domain/Common/Models/AggregateRoot.cs @@ -0,0 +1,31 @@ +namespace Lumina.Domain.Common.Models; + +/// +/// Base class for all domain Aggregate Roots. +/// +/// The type representing the unique identifier for the Aggregate Root. It should be a non-nullable type. +public abstract class AggregateRoot : Entity where TId : notnull, AggregateRootId + where TIdType : notnull +{ + #region ==================================================================== PROPERTIES ================================================================================= + public new AggregateRootId Id { get; protected set; } + #endregion + + #region ====================================================================== CTOR ===================================================================================== + /// + /// Overload C-tor. + /// + /// The id of the entity. + protected AggregateRoot(TId id) : base(id) + { + Id = id; + } + +#pragma warning disable CS8618 + protected AggregateRoot() // only needed during reflection + { + + } +#pragma warning restore CS8618 + #endregion +} \ No newline at end of file diff --git a/src/Lumina.Domain/Common/Models/AggregateRootId.cs b/src/Lumina.Domain/Common/Models/AggregateRootId.cs new file mode 100644 index 00000000..b3b35377 --- /dev/null +++ b/src/Lumina.Domain/Common/Models/AggregateRootId.cs @@ -0,0 +1,12 @@ +namespace Lumina.Domain.Common.Models; + +/// +/// Base identity class for all domain Aggregate Roots. +/// +/// The type representing the unique identifier for the Aggregate Root. It should be a non-nullable type. +public abstract class AggregateRootId : ValueObject where TId : notnull +{ + #region ==================================================================== PROPERTIES ================================================================================= + public abstract TId Value { get; protected set; } + #endregion +} \ No newline at end of file diff --git a/src/Lumina.Domain/Common/Models/Entity.cs b/src/Lumina.Domain/Common/Models/Entity.cs new file mode 100644 index 00000000..831e3bd8 --- /dev/null +++ b/src/Lumina.Domain/Common/Models/Entity.cs @@ -0,0 +1,83 @@ +namespace Lumina.Domain.Common.Models; + +/// +/// 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 ================================================================================= + public TId Id { get; protected set; } + #endregion + + #region ====================================================================== CTOR ===================================================================================== + /// + /// Overload C-tor. + /// + /// The id of the entity. + protected Entity(TId id) + { + Id = id; + } + +#pragma warning disable CS8618 + 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 +} \ No newline at end of file diff --git a/src/Lumina.Domain/Common/Models/ValueObject.cs b/src/Lumina.Domain/Common/Models/ValueObject.cs new file mode 100644 index 00000000..609e3490 --- /dev/null +++ b/src/Lumina.Domain/Common/Models/ValueObject.cs @@ -0,0 +1,71 @@ +namespace Lumina.Domain.Common.Models; + +/// +/// Base class for all domain Value Objects. +/// +/// The type representing the unique identifier for the Entity. It should be a non-nullable type. +public abstract class ValueObject : IEquatable +{ + #region ===================================================================== METHODS =================================================================================== + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public abstract IEnumerable GetEqualityComponents(); + + /// + /// 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(ValueObject? other) + { + return Equals((object?)other); + } + + /// + /// Determines whether the specified object is equal to the current object. + /// + /// The object to compare with the current object. + /// if the specified object is equal to the current object, otherwise. + public override bool Equals(object? obj) + { + if (obj is null || obj.GetType() != GetType()) + return false; + var valueObject = (ValueObject)obj; + return GetEqualityComponents().SequenceEqual(valueObject.GetEqualityComponents()); + } + + /// + /// 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 ==(ValueObject left, ValueObject 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 !=(ValueObject left, ValueObject right) + { + return !Equals(left, right); + } + + /// + /// Override of the default hash function. + /// + /// A hash code for the current object. + public override int GetHashCode() + { + return GetEqualityComponents().Select(x => x?.GetHashCode() ?? 0) + .Aggregate((x, y) => x ^ y); + } + #endregion +} \ No newline at end of file