Created
February 10, 2020 05:31
-
-
Save sabbiryan/8d2ed32c177909bd679a476a055e75eb to your computer and use it in GitHub Desktop.
Dynamic object property value compare. Compare two object of same model type with different value and get the changed and matched property collection in a response with status.
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 static class ComparerExtensions | |
{ | |
public static ObjectCompareResponse DeepCompare(this object source, object comparer) | |
{ | |
var response = new ObjectCompareResponse(); | |
if (ReferenceEquals(source, comparer)) return response; | |
if ((source == null) || (comparer == null)) | |
{ | |
response.MarkAsNotEqual(); | |
return response; | |
} | |
//Compare two object's class, return false if they are difference | |
if (source.GetType() != comparer.GetType()) | |
{ | |
response.MarkAsNotEqual(); | |
return response; | |
} | |
//Get all properties of source | |
//And compare each with comparer | |
foreach (var property in source.GetType().GetProperties()) | |
{ | |
var sourceValue = property.GetValue(source); | |
var comparerValue = property.GetValue(comparer); | |
if (sourceValue == null) | |
{ | |
if (comparerValue == null) | |
{ | |
response.AddToEqual(new ObjectCompareMatchProperty() | |
{ | |
Name = property, | |
Value = null | |
}); | |
} | |
else | |
{ | |
response.AddToChange(new ObjectCompareChangeProperty() | |
{ | |
Name = property, | |
OldValue = null, | |
NewValue = comparerValue | |
}); | |
} | |
continue; | |
} | |
if (!sourceValue.Equals(comparerValue)) | |
{ | |
response.AddToChange(new ObjectCompareChangeProperty() | |
{ | |
Name = property, | |
OldValue = sourceValue, | |
NewValue = comparerValue | |
}); | |
} | |
else | |
{ | |
response.AddToEqual(new ObjectCompareMatchProperty() | |
{ | |
Name = property, | |
Value = sourceValue | |
}); | |
} | |
} | |
return response; | |
} | |
} |
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 class ObjectCompareChangeProperty | |
{ | |
public PropertyInfo Name { get; set; } | |
public object OldValue { get; set; } | |
public object NewValue { get; set; } | |
} | |
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 class ObjectCompareMatchProperty | |
{ | |
public PropertyInfo Name { get; set; } | |
public object Value { get; set; } | |
} |
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 class ObjectCompareResponse | |
{ | |
private bool IsEquality { get; set; } = true; | |
public bool IsEqual => IsEquality && ChangeProperties.Count == 0; | |
public List<ObjectCompareMatchProperty> EqualProperties { get; set; } = new List<ObjectCompareMatchProperty>(); | |
public List<ObjectCompareChangeProperty> ChangeProperties { get; set; } = new List<ObjectCompareChangeProperty>(); | |
public void MarkAsEqual() | |
{ | |
this.IsEquality = true; | |
} | |
public void MarkAsNotEqual() | |
{ | |
this.IsEquality = false; | |
} | |
public void AddToEqual(ObjectCompareMatchProperty property) | |
{ | |
this.EqualProperties.Add(property); | |
} | |
public void AddToChange(ObjectCompareChangeProperty property) | |
{ | |
this.ChangeProperties.Add(property); | |
} | |
} | |
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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Create the two student object | |
var obj1 = new Student | |
{ | |
FirstName = "Sabbir", | |
LastName = "Ahamed", | |
Email = "[email protected]" | |
} | |
var obj2 = new Student | |
{ | |
FirstName = "Foyzul", | |
LastName = "Karim", | |
Email = "[email protected]" | |
} | |
//How to user it | |
var compareResponse = obj1.DeepCompare(obj2); | |
if (!compareResponse.IsEqual) | |
{ | |
foreach (var changeProperty in compareResponse.ChangeProperties) | |
{ | |
var oldVValue = changeProperty.OldValue?.ToString(); | |
var newVValue = changeProperty.NewValue?.ToString(); | |
Console.WriteLine($"Old value: {oldVValue} \t New value: {newVValue}"); | |
} | |
} | |
} | |
} |
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 class Student | |
{ | |
public string FirstName {get; set;} | |
public string LastName {get; set;} | |
public string Email {get; set;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment