How to ignore object properties?

Aug 30, 2013 at 7:14 PM
Edited Aug 30, 2013 at 7:24 PM
I'm trying to figure out how to ignore properties for my objects to compare.

I tried the following with no luck:
                    Configuration config = Configuration.Default;
                    config.CheckIgnoreMember(typeof (Insured).GetProperty("InsertDate"));
                    config.CheckIgnoreMember(typeof(Insured).GetProperty("LastModifiedDate"));
                    Snapshot s1 = new ObjectSnapshot(clonedObject);
                    Snapshot s2 = new ObjectSnapshot(originalPolicyTranObject);

                    CompareItem ci = new ObjectCompareItem();
                    ci.Create(s1, s2);
                    ChangeSet cs = new ChangeSet();
                    cs.Create(ci);
                    var flat = cs.Flatten();
Seems like the configuration changes are not passed to the comparer. Also I tried excluding my properties at the Snapshot level but didn't find a way to manually set the context or change it for that SnapShot object... By looking at the code it is only using defaults.

Any help appreciated.
Coordinator
Sep 6, 2013 at 5:44 PM
Currently returned from vacation :)

Hi adolfo - I'll have look at that in the next hours and give you a solution.
Coordinator
Sep 6, 2013 at 8:26 PM
Edited Sep 6, 2013 at 8:50 PM
Modifying the CheckIgnoreMember property wasn't intended, till now.
My intense was, that you can
  • compare objects respecting all properties
  • compare objects respecting selected properties (by tagging them with CompareAttribute)
  • compare objects respecting selected properties (by setting them via an external configuration file) <<< this is currently progress
But yours is an interesting approach for steering the snap shot creation and I agree, it is useful. :) Thanks adolfo1981!

I've created and solved issues 1498 (https://objectcomparer.codeplex.com/workitem/1498) and 1499 (https://objectcomparer.codeplex.com/workitem/1499). Also I've created a unit test (Project:ObjectComparer.Tests > Class:ConfigurationTest > Method:DefaultCheckIgnorePropertyTest).

Now it is possible to create a context object, set it up and pass it for snap shot creation.
The subjected unit test shows how you can modify the context and also the CheckIgnoreMember delegate.

Let's take a look at that example:

First, you've to instantiate a context object:
var myContext = Context.GetDefaultContext();
Second, you've to replace the default delegate for CheckIgnoreMember
Frame:
myContext.Configuration.CheckIgnoreMember = new CheckIgnoreMemberDelegate(delegate(object o) {
var retval = true;
// ... your decisions go here ...
return retval;
});
Example:
myContext.Configuration.CheckIgnoreMember = new CheckIgnoreMemberDelegate(delegate(object o) {
                var ignore = false;
                //
                // Check to ignore by name of member (field or property)
                //
                var memberNameToCheck = string.Empty;
                if (o is PropertyInfo) {
                    memberNameToCheck = (o as PropertyInfo).Name;

                } else if (o is FieldInfo) {
                    memberNameToCheck = (o as FieldInfo).Name;

                } else if (o is string)
                    memberNameToCheck = o as string;
                
                ignore |= string.Equals(memberNameToCheck, "Number");

                //
                // Check to ignore by PropertyInfo
                //
                if (o is PropertyInfo) {
                    var pi = o as PropertyInfo;
                    ignore |= pi == typeof(Order).GetProperty("Customer");
                        
                }
                //
                // Return value
                //
                return ignore;

            });
Third, you've to pass the context to the neccessary methods
            ss1 = new ObjectSnapshot(yourObject, myContext);
            // modify "yourObject"
            ss2 = new ObjectSnapshot(yourObject, myContext);

            ci = new ObjectCompareItem();
            ci.Create(ss1, ss2);
            cs = new ChangeSet();
            cs.Create(ci);
Marked as answer by testy on 4/8/2014 at 4:37 PM
Sep 6, 2013 at 8:54 PM
Thanks for the quick response and fix!! I'll try it out as soon as possible. I really like your project by the way.
Coordinator
Sep 10, 2013 at 9:27 AM
:) cool, thanks!
Please give me your Feedback, if it fits for you.
Sep 10, 2013 at 12:34 PM
Just tried it and it works fine. Thanks again!
Coordinator
Sep 17, 2013 at 11:24 PM
I've tested using the configuration/context more than once (like the solution I told you) and found out, that when not creating new instances between snapshot creations all subsequent snapshots contain incomplete data. I'll fix this soon (linked issue: https://objectcomparer.codeplex.com/workitem/1507)
May 21, 2014 at 3:36 PM
Hi,

Is it possible to make Context.GetDefaultContext method public?
       internal static Context GetDefaultContext() {
When I call Context.Default it is always returning me the same Context once it creates it:
public static Context Default {
            get {
                if( _default==null )
                    _default = GetDefaultContext();

                return _default;

            }
        }
And I need a new context every time I call the compare.

Thanks,
Coordinator
May 22, 2014 at 10:31 PM
Hi,

I think the easiest way is to create a new context-object:
see: https://objectcomparer.codeplex.com/discussions/546299
May 27, 2014 at 12:45 PM
I just saw the new thread. That solution worked, thanks again for your help and quick response.