Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
Created August 11, 2018 10:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkhorikov/36ebca588ea0d11b1979419f095db18f to your computer and use it in GitHub Desktop.
Save vkhorikov/36ebca588ea0d11b1979419f095db18f to your computer and use it in GitHub Desktop.
Base entity class
public abstract class Entity
{
public virtual long Id { get; protected set; }
protected virtual object Actual => this;
public override bool Equals(object obj)
{
var other = obj as Entity;
if (other is null)
return false;
if (ReferenceEquals(this, other))
return true;
if (Actual.GetType() != other.Actual.GetType())
return false;
if (Id == 0 || other.Id == 0)
return false;
return Id == other.Id;
}
public static bool operator ==(Entity a, Entity b)
{
if (a is null && b is null)
return true;
if (a is null || b is null)
return false;
return a.Equals(b);
}
public static bool operator !=(Entity a, Entity b)
{
return !(a == b);
}
public override int GetHashCode()
{
return (Actual.GetType().ToString() + Id).GetHashCode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment