I have a site where users upload data into it and I only want to update data where properties have been changed. So I am comparing 2 objects of the same type for changes and I need to exclude a few properties such as ModifiedOn which is a date.
Here is my code thus far using reflection:
private bool hasChanges(object OldObject, object newObject)
{
var oldprops = (from p in OldObject.GetType().GetProperties() select p).ToList();
var newprops = (from p in newObject.GetType().GetProperties() select p).ToList();
bool isChanged = false;
foreach (PropertyInfo i in oldprops)
{
if (checkColumnNames(i.Name))
{
var newInfo = (from x in newprops where x.Name == i.Name select x).Single();
var oldVal = i.GetValue(OldObject, null);
var newVal = newInfo.GetValue(newObject, null);
if (newVal == null || oldVal == null)
{
if (newVal == null && oldVal != null)
{
isChanged = true;
return true;
}
if (oldVal == null && newVal != null)
{
isChanged = true;
return true;
}
}
else
{
if (!newVal.Equals(oldVal))
{
isChanged = true;
return true;
}
}
}
}
return isChanged;
}
I ignore certain columns with this method:
private bool checkColumnNames(string colName)
{
if (
colName.ToLower() == "productid" ||
colName.ToLower() == "customerid" ||
colName.ToLower() == "shiptoid" ||
colName.ToLower() == "parentchildid" ||
colName.ToLower() == "categoryitemid" ||
colName.ToLower() == "volumepricingid" ||
colName.ToLower() == "tagid" ||
colName.ToLower() == "specialprice" ||
colName.ToLower() == "productsmodifierid" ||
colName.ToLower() == "modifierlistitemid" ||
colName.ToLower() == "modifierlistid" ||
colName.ToLower() == "categoryitemid" ||
colName.ToLower() == "createdon" ||
colName.ToLower() == "createdby" ||
colName.ToLower() == "modifiedon" ||
colName.ToLower() == "modifiedby" ||
colName.ToLower() == "deletedon" ||
colName.ToLower() == "deletedby" ||
colName.ToLower() == "appendproductmodifiers" ||
colName.ToLower() == "introdate" ||
colName.ToLower() == "id" ||
colName.ToLower() == "discontinued" ||
colName.ToLower() == "stagingcategories"
)
return false;
return true;
}
This has been working very well except for now I have users comparing 50,000+ items in a single upload which is taking a really long time.
Is there a faster way to accomplish this?
Are the objects guaranteed to have the same type? Even if not, you could check them, and if they are the same type, send them to this method:
This is only slightly more efficient than your method. As Tim Medora and PinnyM noted, it would be quicker still to emit code dynamically and cache the result, which would mean that you take the reflection hit only once, rather than once per object.
Note also that according to Best Practices for Using Strings in the .NET Framework, you should be using ToUpper rather than ToLower for your string comparisons, but you should be using
String.Equals(string, string, StringComparison)
rather than converting the case yourself. That will have one advantage, at least: Equals returns false if the strings are different lengths, so you skip the case conversion. That will save a bit of time as well.Another thought:
If the objects are of different types, you can still improve your algorithm by joining the properties collections rather than using the repeated linear search you have:
Final thought (inspired by Tim Schmelter's comment):
Override
object.Equals
on your classes, and useSample class:
It would certainly be faster if you just used reflection to create and compile a method using the above logic. This should be much faster than reflecting on each object.
Compile and cache your code using expression trees or dynamic methods. You will probably see a 10-100x performance improvement. Your original reflection code to retrieve properties and you can use it as a basis for creating a compiled version.
Example
Here is a snippet of code I use in a framework for reading all of the properties of an object to track state changes. In this scenario, I do not know any of the property names of the object. All of the property values are placed into a
StringBuilder
.I've simplified this from my original code; it still compiles, but you may need to tweak it.
Note that if you are not familiar with IL generation, most people find expression trees much easier to work with. Either approach has a similar result.