Introduction
ZCompare is a powerful autonomous typesafe .NET object comparison tool. The modified data scenario where changes have been made to the original data is a common problem and essentially, as developers, we want to know what has been added, deleted, or modified. I am going to show how easy ZCompare can make detecting these changes and working with the 'before' and 'after' conditions of each item of data.
Background
I have my original data from the database, and my user has made many changes to this data which reflects in my complex .NET object. I want to know if they have made changes to data or created new data or deleted data. More importantly, I want to know what exactly has changed and what it has changed from. Sounds familiar?
The Modified Data Scenario
Let's assume we have a large object graph; a list of Suppliers for example. See here for class definitions.
Let's assume that originalSuppliers is a list of current suppliers from the database. We are creating them here on the fly for simplicity, using a helper class 'SampleData'.
I am using sample data from the Zaybu.Compare.Data assembly is included in the ZCee Project.
- List < Supplier > originalSuppliers = SampleData.CreateSuppliers(8);
- // updatedSuppliers contain all of our modifications. // We create them here on the fly and then we are going to modify them.
- List < Supplier > updatedSuppliers = SampleData.CreateSuppliers(8); // We are going to make some modifications to our data - updatedSuppliers
- updatedSuppliers.RemoveAt(0); // Delete a supplier
- updatedSuppliers[0].Products[2].Description = "Description Updated";
- updatedSuppliers[1].Products[1].Price = 19.99 f;
- updatedSuppliers[4].Status = SupplierStatus.Active;
- updatedSuppliers.Add(SampleData.CreateSupplier(9)); // Create a new one // Let's compare them ZCompareResults results = ZCompare.Compare(originalSuppliers, updatedSuppliers);
'results' is a container for all the results. We will use this object to query for the results you want to work with.
Let's get only those suppliers that have changes made to them. We use the results.GetResults<T>() method.
- // Pass in a reference object, in this case the top level 'originalSuppliers' list // and also 'true' to say we only want Suppliers that have changes made to them
- var supplierResults = results.GetResults < Supplier > (originalSuppliers, true); // Lets see if we can do something useful with the results
- supplierResults.ForEach(s => {
- if (s.Status == ResultStatus.Added) {
- Debug.WriteLine("This supplier has been added: " + s.ChangedToValue.Name);
- } else if (s.Status == ResultStatus.Deleted) {
- Debug.WriteLine("This supplier is deleted: " + s.OriginalValue.Name);
- } else {
- Debug.WriteLine("This supplier has been changed: " + s.ChangedToValue.Name);
- }
- });
- --Debug Output--
- This supplier is deleted: Complicated Cow
- This supplier has been changed: Cloudy Cat
- This supplier has been changed: Intelligent Duck
- This supplier has been changed: Stormy Knife Company
- This supplier has been added: The Cheeky Panda Company
Note how each result has a 'Status' flag indicating if the Supplier has been added, deleted, or modified. Also, each result has an 'OriginalValue' and a 'ChangedToValue' property which references the original object before changes and the modified object respectively.
- supplierResults.ForEach(s => {
- if (s.Status == ResultStatus.Added) {
- ORM.Insert(s.ChangedToValue);
- } else if (s.Status == ResultStatus.Deleted) {
- ORM.Delete < Supplier > (s.OriginalValue.ID);
- } else {
- ORM.Update(s.ChangedToValue);
- }
- });
Of course, things are rarely this simple. We have a Supplier object with a list of Products and a dictionary of Addresses as properties. These properties may have changed and we want to work with them as 'Products' and 'Address' types.
So, let's go again and recreate our data and modify it again a bit differently.
Gayathri AnbazhaganPosted Jun 24, 2017, 8:43 AM
Good start....keep sharing....