-
-
Save vkhorikov/36ebca588ea0d11b1979419f095db18f to your computer and use it in GitHub Desktop.
Base entity class
This file contains hidden or 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 | |
{ | |
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