mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Implements the basic common structure of Domain objects
This commit is contained in:
1 parent
3f376d0630
commit
1667036009
4 files changed
+197
No files matched your search
@@ -0,0 +1,31 @@
|
||||
namespace Lumina.Domain.Common.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for all domain Aggregate Roots.
|
||||
/// </summary>
|
||||
/// <typeparam name="TId">The type representing the unique identifier for the Aggregate Root. It should be a non-nullable type.</typeparam>
|
||||
public abstract class AggregateRoot<TId, TIdType> : Entity<TId> where TId : notnull, AggregateRootId<TIdType>
|
||||
where TIdType : notnull
|
||||
{
|
||||
#region ==================================================================== PROPERTIES =================================================================================
|
||||
public new AggregateRootId<TIdType> Id { get; protected set; }
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Overload C-tor.
|
||||
/// </summary>
|
||||
/// <param name="id">The id of the entity.</param>
|
||||
protected AggregateRoot(TId id) : base(id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
#pragma warning disable CS8618
|
||||
protected AggregateRoot() // only needed during reflection
|
||||
{
|
||||
|
||||
}
|
||||
#pragma warning restore CS8618
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Lumina.Domain.Common.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Base identity class for all domain Aggregate Roots.
|
||||
/// </summary>
|
||||
/// <typeparam name="TId">The type representing the unique identifier for the Aggregate Root. It should be a non-nullable type.</typeparam>
|
||||
public abstract class AggregateRootId<TId> : ValueObject where TId : notnull
|
||||
{
|
||||
#region ==================================================================== PROPERTIES =================================================================================
|
||||
public abstract TId Value { get; protected set; }
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
namespace Lumina.Domain.Common.Models;
|
||||
|
||||
/// <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
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
namespace Lumina.Domain.Common.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for all domain Value Objects.
|
||||
/// </summary>
|
||||
/// <typeparam name="TId">The type representing the unique identifier for the Entity. It should be a non-nullable type.</typeparam>
|
||||
public abstract class ValueObject : IEquatable<ValueObject>
|
||||
{
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Gets the list of items that define equality of the object.
|
||||
/// </summary>
|
||||
/// <returns>A list of items defining the equality.</returns>
|
||||
public abstract IEnumerable<object> GetEqualityComponents();
|
||||
|
||||
/// <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(ValueObject? other)
|
||||
{
|
||||
return Equals((object?)other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified object is equal to the current object.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to compare with the current object.</param>
|
||||
/// <returns><see langword="true"/> if the specified object is equal to the current object, <see langword="false"/> otherwise.</returns>
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj is null || obj.GetType() != GetType())
|
||||
return false;
|
||||
var valueObject = (ValueObject)obj;
|
||||
return GetEqualityComponents().SequenceEqual(valueObject.GetEqualityComponents());
|
||||
}
|
||||
|
||||
/// <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 ==(ValueObject left, ValueObject 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 !=(ValueObject left, ValueObject 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 GetEqualityComponents().Select(x => x?.GetHashCode() ?? 0)
|
||||
.Aggregate((x, y) => x ^ y);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in new issue
Block a user