Created
March 22, 2022 12:48
-
-
Save kierunb/a868097c824805cf50d98a521735720a to your computer and use it in GitHub Desktop.
ddd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class ValueObject | |
| { | |
| protected static bool EqualOperator(ValueObject left, ValueObject right) | |
| { | |
| if (ReferenceEquals(left, null) ^ ReferenceEquals(right, null)) | |
| { | |
| return false; | |
| } | |
| return ReferenceEquals(left, null) || left.Equals(right); | |
| } | |
| protected static bool NotEqualOperator(ValueObject left, ValueObject right) | |
| { | |
| return !(EqualOperator(left, right)); | |
| } | |
| protected abstract IEnumerable<object> GetEqualityComponents(); | |
| public override bool Equals(object obj) | |
| { | |
| if (obj == null || obj.GetType() != GetType()) | |
| { | |
| return false; | |
| } | |
| var other = (ValueObject)obj; | |
| return this.GetEqualityComponents().SequenceEqual(other.GetEqualityComponents()); | |
| } | |
| public override int GetHashCode() | |
| { | |
| return GetEqualityComponents() | |
| .Select(x => x != null ? x.GetHashCode() : 0) | |
| .Aggregate((x, y) => x ^ y); | |
| } | |
| public ValueObject GetCopy() | |
| { | |
| return this.MemberwiseClone() as ValueObject; | |
| } | |
| } | |
| public abstract class Entity | |
| { | |
| int? _requestedHashCode; | |
| int _Id; | |
| public virtual int Id | |
| { | |
| get | |
| { | |
| return _Id; | |
| } | |
| protected set | |
| { | |
| _Id = value; | |
| } | |
| } | |
| private List<INotification> _domainEvents; | |
| public IReadOnlyCollection<INotification> DomainEvents => _domainEvents?.AsReadOnly(); | |
| public void AddDomainEvent(INotification eventItem) | |
| { | |
| _domainEvents = _domainEvents ?? new List<INotification>(); | |
| _domainEvents.Add(eventItem); | |
| } | |
| public void RemoveDomainEvent(INotification eventItem) | |
| { | |
| _domainEvents?.Remove(eventItem); | |
| } | |
| public void ClearDomainEvents() | |
| { | |
| _domainEvents?.Clear(); | |
| } | |
| public bool IsTransient() | |
| { | |
| return this.Id == default(Int32); | |
| } | |
| public override bool Equals(object obj) | |
| { | |
| if (obj == null || !(obj is Entity)) | |
| return false; | |
| if (Object.ReferenceEquals(this, obj)) | |
| return true; | |
| if (this.GetType() != obj.GetType()) | |
| return false; | |
| Entity item = (Entity)obj; | |
| if (item.IsTransient() || this.IsTransient()) | |
| return false; | |
| else | |
| return item.Id == this.Id; | |
| } | |
| public override int GetHashCode() | |
| { | |
| if (!IsTransient()) | |
| { | |
| if (!_requestedHashCode.HasValue) | |
| _requestedHashCode = this.Id.GetHashCode() ^ 31; // XOR for random distribution (http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx) | |
| return _requestedHashCode.Value; | |
| } | |
| else | |
| return base.GetHashCode(); | |
| } | |
| public static bool operator ==(Entity left, Entity right) | |
| { | |
| if (Object.Equals(left, null)) | |
| return (Object.Equals(right, null)) ? true : false; | |
| else | |
| return left.Equals(right); | |
| } | |
| public static bool operator !=(Entity left, Entity right) | |
| { | |
| return !(left == right); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment