Created
September 1, 2015 16:25
-
-
Save DavidBrower/5861cee08f2d07c0904c to your computer and use it in GitHub Desktop.
Entity Base Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Entity<TId> : IEquatable<Entity<TId>> | |
{ | |
private readonly TId _id; | |
protected Entity(TId id) | |
{ | |
if (object.Equals(id, default(TId))) | |
{ | |
throw new ArgumentException("The ID cannot be the default value.", nameof(id)); | |
} | |
this._id = id; | |
} | |
public TId Id | |
{ | |
get { return this._id; } | |
} | |
public override bool Equals(object obj) | |
{ | |
var entity = obj as Entity<TId>; | |
if (entity != null) | |
{ | |
return this.Equals(entity); | |
} | |
return base.Equals(obj); | |
} | |
public override int GetHashCode() | |
{ | |
return this.Id.GetHashCode(); | |
} | |
public bool Equals(Entity<TId> other) | |
{ | |
if (other == null) | |
{ | |
return false; | |
} | |
return this.Id.Equals(other.Id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment