mardi 3 mai 2016

Comparing an Entity

I've got an entity that I build, I take an instantiated entity and a modified entity. This allows me to hold the initial data, to compare against the modified data. The question, is what would be the ideal approach? Should I implement IEquatable as an override on Object.Equals or implement ICompare? My original implementation was:

var properties = typeof(TEntity).GetProperties();
foreach(var property in properties)
{
    var initialEntity = original.GetType().GetProperty(property.Name).GetValue(original, null);    
    var modifiedEntity = userChange.GetType().GetProperty(property.Name).GetValue(userChange, null);

    if(initialEntity.Equals(modifiedEntity) == false && !ignore.Contains(property.Name))
    {
        // Do Something
    }
}

My understanding was that it would return a boolean, also in this instance it would compare on Value Equality, I'm assuming though it is comparing based on reference equality.

Because it never distinguishes, it remains equal under all circumstances.





Aucun commentaire:

Enregistrer un commentaire