diff --git a/src/DigitalData.EmailProfiler.Domain/Common/BaseEntity.cs b/src/DigitalData.EmailProfiler.Domain/Common/BaseEntity.cs new file mode 100644 index 0000000..50e7d15 --- /dev/null +++ b/src/DigitalData.EmailProfiler.Domain/Common/BaseEntity.cs @@ -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; } +} diff --git a/src/DigitalData.EmailProfiler.Domain/Common/IAggregateRoot.cs b/src/DigitalData.EmailProfiler.Domain/Common/IAggregateRoot.cs new file mode 100644 index 0000000..a7bdc76 --- /dev/null +++ b/src/DigitalData.EmailProfiler.Domain/Common/IAggregateRoot.cs @@ -0,0 +1,8 @@ +namespace DigitalData.EmailProfiler.Domain.Common; + +/// +/// Marker interface for aggregate roots in DDD +/// +public interface IAggregateRoot +{ +} diff --git a/src/DigitalData.EmailProfiler.Domain/Common/ValueObject.cs b/src/DigitalData.EmailProfiler.Domain/Common/ValueObject.cs new file mode 100644 index 0000000..f741767 --- /dev/null +++ b/src/DigitalData.EmailProfiler.Domain/Common/ValueObject.cs @@ -0,0 +1,43 @@ +namespace DigitalData.EmailProfiler.Domain.Common; + +/// +/// Base class for value objects that implement equality by value +/// +public abstract class ValueObject +{ + protected abstract IEnumerable 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); + } +}