-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.cs
62 lines (49 loc) · 1.48 KB
/
Entity.cs
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
namespace Ddd.Domain;
public class Entity<TId> : Aggregate where TId : struct
{
private int? _requestedHashCode;
private TId _id;
public virtual TId Id
{
get { return _id; }
protected set { _id = value; }
}
public override bool IsTransient()
{
return this.Id.Equals(default(TId));
}
public override bool Equals(object obj)
{
if (!(obj is Entity<TId>))
return false;
if (Object.ReferenceEquals(this, obj))
return true;
if (this.GetType() != obj.GetType())
return false;
Entity<TId> item = (Entity<TId>)obj;
return item.Id.Equals(this.Id);
}
public static bool operator ==(Entity<TId> left, Entity<TId> right)
{
if (Object.Equals(left, null))
return (Object.Equals(right, null)) ? true : false;
else
return left.Equals(right);
}
public static bool operator !=(Entity<TId> left, Entity<TId> right)
{
return !(left == right);
}
public override int GetHashCode()
{
if (!IsTransient())
{
// XOR for random distribution
// (http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx)
if (!_requestedHashCode.HasValue)
_requestedHashCode = this.Id.GetHashCode() ^ 31;
return _requestedHashCode.Value;
}
return base.GetHashCode();
}
}