I have a web service that returns a complex data structure representing the results of an intensive process. To make the service responsive, the process is run in an asynchronous Task.
The first time the service is called, an empty data structure is returned and the Task started. Subsequent service calls return the cached data until the Task completes. When finished, I need to determine if the results actually changed. If so, I update a property that indicates the date/time the results were updated and reset the cached data so the new structure is returned by the service. At some point in the future, based on a variety of reasons, the Task is restarted and the logic repeats.
I am looking for the most efficient way of determining whether the results have changed from the ‘cached’ data. The simplest would be to implement Equals on every object in the structure with parent’s equality based on their children’s equality, etc. so calling Equals on the root objects would yield the desired result, but this requires crawling the entire structure and I’m not sure this is the best approach.
I also thought about starting with a copy of the original and using ‘IsDirty’ flags to indicate that a object in the structure has changed but worry that the copy operation could negate any performce benefits.
What approach would you use (and why)?
I would create a static method that recursively walks over all properties of 2 instances ( left and right ), which optionally can be controlled by using attributes, and calls the equals method of left over right. When one of the properties indicate inequality, we know that the 2 instances are different. This allows me to implement a minimum amount of code for comparison yet be fully in control in what gets compared and how it gets compared (by marking certain properties with a new attribute like [NoCompare] and by implementing custom equality logic).