Taylohtio/IDP/parsers/parser-management-fee/common/domain/Entity.cs

95 lines
2.3 KiB
C#

using Microsoft.Azure.CosmosDB.Table;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace taloyhtio.idp.parser.common.domain
{
public class Entity : TableEntity
{
int? _requestedHashCode;
Guid _id;
public Guid Id
{
get { return _id; }
set
{
_id = value;
RowKey = value.ToString();
}
}
private List<INotification> _domainEvents;
[JsonIgnore]
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 Guid.Empty.Equals(Id);
}
public override bool Equals(object obj)
{
if (obj == null || !(obj is Entity))
return false;
if (ReferenceEquals(this, obj))
return true;
if (GetType() != obj.GetType())
return false;
Entity item = (Entity)obj;
if (item.IsTransient() || IsTransient())
return false;
else
return item.Id == Id;
}
public override int GetHashCode()
{
if (!IsTransient())
{
if (!_requestedHashCode.HasValue)
_requestedHashCode = Id.GetHashCode() ^ 31;
return _requestedHashCode.Value;
}
else
return base.GetHashCode();
}
public static bool operator ==(Entity left, Entity right)
{
if (Equals(left, null))
return Equals(right, null);
else
return left.Equals(right);
}
public static bool operator != (Entity left, Entity right)
{
return !(left == right);
}
}
}