Skip to content

Commit

Permalink
feat(baseEntity) : add notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikoo-Asadnejad committed Nov 2, 2023
1 parent 8ee1ba9 commit 07cdfea
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
88 changes: 88 additions & 0 deletions Src/Product.Domain/Base/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace Product.Domain.Entities;

public class BaseEntity
{
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();

Check warning on line 20 in Src/Product.Domain/Base/BaseEntity.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

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;
}

public override bool Equals(object obj)

Check warning on line 43 in Src/Product.Domain/Base/BaseEntity.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).
{
if (!(obj is BaseEntity))
return false;

if (Object.ReferenceEquals(this, obj))
return true;

if (this.GetType() != obj.GetType())
return false;

BaseEntity item = (BaseEntity)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 ==(BaseEntity left, BaseEntity right)
{
if (Object.Equals(left, null))
return (Object.Equals(right, null)) ? true : false;
else
return left.Equals(right);
}
public static bool operator !=(BaseEntity left, BaseEntity right)
{
return !(left == right);
}
}


6 changes: 6 additions & 0 deletions Src/Product.Domain/Base/INotification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Product.Domain.Entities;

public interface INotification
{

}

0 comments on commit 07cdfea

Please sign in to comment.