Project Description
ObjectComparer is a small Microsoft .Net 2.0 library containing classes to run the following Microsoft .Net Reflection based operations on Microsoft .Net objects.
1. Create snapshots of (ANY) objects
2. Compare snapshots of objects and compare objects
3. Create deep copy of objects
4. Create structured diffs of objects
This enables devlopers to create audit trails based on objects.
In future there will be a new project implementing audit trail functionality.
You can also use the NuGet package: https://nuget.org/packages/SIT.ObjectComparer/
How to create a structured diff (get the changes)
The sample application shows an Windows Forms UI with data binding to the business object Order.
The Order class contains value type properties and reference type properties like Customer and a list of OrderPosition objects. The Customer object also contains an additional reference to an Address object.
1. Create a snapshot of the Order object (save it’s initial state in the SnapShot object)
In the constructor of the form, a snapshot is created automatically and referenced by the variable _orderSnapshot1.
2. Make changes to the Order object
Next step is to use the UI for making some changes to the order.
3. Calculate the changes and show what has changed (creating the diff)
Next, you click the “Show changes” button while the following code is executed.
3.1. At first, a second snapshot will be created.
3.2 Then, the initial snapshot and the second snapshot just created are used to create a compare item. The compare item holds the property values of two snapshots in a structure where comparison is made easy.
3.3 At last, the compare item is taken an input for creating a change set. The change set holds just the differences between the two snapshots.
After that, the change set is passed to the DiffForm formular.
The change set can be easily displayed as tree view or as flat list and is done by the following snippet.
The example shows the process for creating a structured diff. The interesting thing is, how does the algo check which order positions are new and which are deleted? The solution is, to find a key which identifying an item in the order position list. So, the class OrderPosition is tagged with this attribute.
This attribute is used by the algo to
- show an alternative text referencing changes of the attributed object type and to
- determine if an property is an unique identifier which is used for checking if items in a list were added or removed
Compare without attributes
The drawback of the “Attribute-Solution” is, that the source code of the objects you want to compare has to be in your hand. But you also can compare objects which do not have the attributes – then you just cant compare lists like shown above. An alternate to comparing lists for added and removed items without using attributes is to define the attributes in a type of configuration file. Therefore, the Context class is used which holds a Configuration which holds types and properties to be compared. You also can serialize and deserialize the configuration class. So, you’re able to configure the types and properties to compare without using attributes.