Skip to content

Instantly share code, notes, and snippets.

@DavidBrower
Created September 1, 2015 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBrower/5861cee08f2d07c0904c to your computer and use it in GitHub Desktop.
Save DavidBrower/5861cee08f2d07c0904c to your computer and use it in GitHub Desktop.
Entity Base Class
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