Entity.cs
1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using MediatR;
using System;
using System.Collections.Generic;
using System.Text;
namespace Pole.Domain
{
public abstract class Entity
{
string _id;
public virtual string Id
{
get
{
return _id;
}
protected set
{
_id = value;
}
}
public List<IDomainEvent> DomainEvents { get; private set; }
public bool IsTransient()
{
return string.IsNullOrEmpty( this._id);
}
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()
{
return this.Id.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);
}
public void AddDomainEvent(IDomainEvent eventItem)
{
DomainEvents = DomainEvents ?? new List<IDomainEvent>();
DomainEvents.Add(eventItem);
}
public void RemoveDomainEvent(IDomainEvent eventItem)
{
if (DomainEvents is null) return;
DomainEvents.Remove(eventItem);
}
public void ClearDomainEvents()
{
DomainEvents?.Clear();
}
}
}