Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 11ad96a

Browse files
EntityBase added
1 parent dd29e0c commit 11ad96a

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace AspnetRun.Core.Entities.Base
2+
{
3+
public abstract class EntityBase<TId> : IEntityBase<TId>
4+
{
5+
public virtual TId Id { get; protected set; }
6+
7+
int? _requestedHashCode;
8+
9+
public bool IsTransient()
10+
{
11+
return Id.Equals(default(TId));
12+
}
13+
14+
public override bool Equals(object obj)
15+
{
16+
if (obj == null || !(obj is EntityBase<TId>))
17+
return false;
18+
19+
if (ReferenceEquals(this, obj))
20+
return true;
21+
22+
if (GetType() != obj.GetType())
23+
return false;
24+
25+
var item = (EntityBase<TId>)obj;
26+
27+
if (item.IsTransient() || IsTransient())
28+
return false;
29+
else
30+
return item == this;
31+
}
32+
33+
public override int GetHashCode()
34+
{
35+
if (!IsTransient())
36+
{
37+
if (!_requestedHashCode.HasValue)
38+
_requestedHashCode = Id.GetHashCode() ^ 31; // XOR for random distribution (http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx)
39+
40+
return _requestedHashCode.Value;
41+
}
42+
else
43+
return base.GetHashCode();
44+
}
45+
46+
public static bool operator ==(EntityBase<TId> left, EntityBase<TId> right)
47+
{
48+
if (Equals(left, null))
49+
return Equals(right, null) ? true : false;
50+
else
51+
return left.Equals(right);
52+
}
53+
54+
public static bool operator !=(EntityBase<TId> left, EntityBase<TId> right)
55+
{
56+
return !(left == right);
57+
}
58+
}
59+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /