feat(domain): add common base classes and interfaces
- Add BaseEntity with audit fields (CreatedDate, CreatedBy, ModifiedDate, ModifiedBy) - Add IAggregateRoot marker interface for DDD aggregate roots - Add ValueObject base class with equality comparison by value These classes provide the foundation for all domain entities and value objects.
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
namespace DigitalData.EmailProfiler.Domain.Common;
|
||||||
|
|
||||||
|
public abstract class BaseEntity
|
||||||
|
{
|
||||||
|
public DateTime? CreatedDate { get; set; }
|
||||||
|
public string? CreatedBy { get; set; }
|
||||||
|
public DateTime? ModifiedDate { get; set; }
|
||||||
|
public string? ModifiedBy { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace DigitalData.EmailProfiler.Domain.Common;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Marker interface for aggregate roots in DDD
|
||||||
|
/// </summary>
|
||||||
|
public interface IAggregateRoot
|
||||||
|
{
|
||||||
|
}
|
||||||
43
src/DigitalData.EmailProfiler.Domain/Common/ValueObject.cs
Normal file
43
src/DigitalData.EmailProfiler.Domain/Common/ValueObject.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
namespace DigitalData.EmailProfiler.Domain.Common;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Base class for value objects that implement equality by value
|
||||||
|
/// </summary>
|
||||||
|
public abstract class ValueObject
|
||||||
|
{
|
||||||
|
protected abstract IEnumerable<object> GetEqualityComponents();
|
||||||
|
|
||||||
|
public override bool Equals(object? obj)
|
||||||
|
{
|
||||||
|
if (obj == null || obj.GetType() != GetType())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var other = (ValueObject)obj;
|
||||||
|
return GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return GetEqualityComponents()
|
||||||
|
.Select(x => x?.GetHashCode() ?? 0)
|
||||||
|
.Aggregate((x, y) => x ^ y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator ==(ValueObject? left, ValueObject? right)
|
||||||
|
{
|
||||||
|
if (left is null && right is null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (left is null || right is null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return left.Equals(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator !=(ValueObject? left, ValueObject? right)
|
||||||
|
{
|
||||||
|
return !(left == right);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user