Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>IComparable</code> is for ordering comparisons. Either use <code>IEquatable</code> instead, or just use the static <code>System.Object.Equals</code> method. The latter has the benefit of also working if the object is not a primitive type but still defines its own equality comparison by overriding <code>Equals</code>.</p> <pre><code>object originalValue = property.GetValue(originalObject, null); object newValue = property.GetValue(changedObject, null); if (!object.Equals(originalValue, newValue)) { string originalText = (originalValue != null) ? originalValue.ToString() : "[NULL]"; string newText = (newText != null) ? newValue.ToString() : "[NULL]"; // etc. } </code></pre> <p>This obviously isn't perfect, but if you're only doing it with classes that you control, then you can make sure it always works for your particular needs.</p> <p>There are other methods to compare objects (such as checksums, serialization, etc.) but this is probably the most reliable if the classes don't consistently implement <code>IPropertyChanged</code> and you want to actually know the differences.</p> <hr> <p>Update for new example code:</p> <pre><code>Address address1 = new Address(); address1.StateProvince = new StateProvince(); Address address2 = new Address(); address2.StateProvince = new StateProvince(); IList list = Utility.GenerateAuditLogMessages(address1, address2); </code></pre> <p>The reason that using <code>object.Equals</code> in your audit method results in a "hit" is because the instances are actually not equal!</p> <p>Sure, the <code>StateProvince</code> may be empty in both cases, but <code>address1</code> and <code>address2</code> still have non-null values for the <code>StateProvince</code> property and each instance is different. Therefore, <code>address1</code> and <code>address2</code> have different properties.</p> <p>Let's flip this around, take this code as an example:</p> <pre><code>Address address1 = new Address("35 Elm St"); address1.StateProvince = new StateProvince("TX"); Address address2 = new Address("35 Elm St"); address2.StateProvince = new StateProvince("AZ"); </code></pre> <p>Should these be considered equal? Well, they will be, using your method, because <code>StateProvince</code> does not implement <code>IComparable</code>. That's the only reason why your method reported that the two objects were the same in the original case. Since the <code>StateProvince</code> class does not implement <code>IComparable</code>, the tracker just skips that property entirely. But these two addresses are clearly not equal!</p> <p>This is why I originally suggested using <code>object.Equals</code>, because then you can override it in the <code>StateProvince</code> method to get better results:</p> <pre><code>public class StateProvince { public string Code { get; set; } public override bool Equals(object obj) { if (obj == null) return false; StateProvince sp = obj as StateProvince; if (object.ReferenceEquals(sp, null)) return false; return (sp.Code == Code); } public bool Equals(StateProvince sp) { if (object.ReferenceEquals(sp, null)) return false; return (sp.Code == Code); } public override int GetHashCode() { return Code.GetHashCode(); } public override string ToString() { return string.Format("Code: [{0}]", Code); } } </code></pre> <p>Once you've done this, the <code>object.Equals</code> code will work perfectly. Instead of naïvely checking whether or not <code>address1</code> and <code>address2</code> literally have the same <code>StateProvince</code> reference, it will actually check for semantic equality.</p> <hr> <p>The other way around this is to extend the tracking code to actually descend into sub-objects. In other words, for each property, check the <code>Type.IsClass</code> and optionally the <code>Type.IsInterface</code> property, and if <code>true</code>, then recursively invoke the change-tracking method on the property itself, prefixing any audit results returned recursively with the property name. So you'd end up with a change for <code>StateProvinceCode</code>.</p> <p>I use the above approach sometimes too, but it's easier to just override <code>Equals</code> on the objects for which you want to compare semantic equality (i.e. audit) and provide an appropriate <code>ToString</code> override that makes it clear what changed. It doesn't scale for deep nesting but I think it's unusual to want to audit that way.</p> <p>The last trick is to define your own interface, say <code>IAuditable&lt;T&gt;</code>, which takes a second instance of the same type as a parameter and actually returns a list (or enumerable) of all of the differences. It's similar to our overridden <code>object.Equals</code> method above but gives back more information. This is useful for when the object graph is really complicated and you know you can't rely on Reflection or <code>Equals</code>. You can combine this with the above approach; really all you have to do is substitute <code>IComparable</code> for your <code>IAuditable</code> and invoke the <code>Audit</code> method if it implements that interface.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload