CodeSmith Community
Your Code. Your Way. Faster!

EntityPropertyComparer for complex objects

Latest post 01-18-2007 1:56 PM by Faulcon. 1 replies.
  • 01-17-2007 11:49 AM

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    EntityPropertyComparer for complex objects

    Hi!

    Wouldn't it be a good thing to replace the following method in EntityPropertyComparer.cs with something like thw ones below? This would allow to support complex objects and comparing things like "Customer.AddressIDSource.City" for exasmple, which is not the case at the moment.

    Original code: 

             /// <summary>
            /// Compares 2 objects by their properties, given on the constructor
            /// </summary>
            /// <param name="x">First value to compare</param>
            /// <param name="y">Second value to compare</param>
            /// <returns></returns>
            public int Compare(object x, object y)
            {
                object a = x.GetType().GetProperty(PropertyName).GetValue(x, null);
                object b = y.GetType().GetProperty(PropertyName).GetValue(y, null);

                if ( a != null && b == null )
                    return 1;

                if ( a == null && b != null )
                    return -1;

                if ( a == null && b == null )
                    return 0;

                return ((IComparable)a).CompareTo(b);
            }
     

    New code: 

            public object getObj(object x)
            {
                object a = string.Empty;
                string[] PropertyParts;
                char[] delimiterChars = { '.' };
                PropertyParts = PropertyName.Split(delimiterChars);
                foreach (string propertyPart in PropertyParts)
                {
                    a = x.GetType().GetProperty(propertyPart).GetValue(x, null);
                    x = a;
                }

                return a;
            }

            /// <summary>
            /// Compares 2 objects by their properties, given on the constructor
            /// </summary>
            /// <param name="x">First value to compare</param>
            /// <param name="y">Second value to compare</param>
            /// <returns></returns>
            public int Compare(object x, object y)
            {
                //object a = x.GetType().GetProperty(PropertyName).GetValue(x, null);
                //object b = y.GetType().GetProperty(PropertyName).GetValue(y, null);
                object a = getObj(x);
                object b = getObj(y);

                if ( a != null && b == null )
                    return 1;

                if ( a == null && b != null )
                    return -1;

                if ( a == null && b == null )
                    return 0;

                return ((IComparable)a).CompareTo(b);
            }

    Cheers!

    JF
    • Post Points: 35
  • 01-18-2007 1:56 PM In reply to

    • Faulcon
    • Top 150 Contributor
    • Joined on 12-12-2005
    • Posts 32
    • Points 1,040

    Re: EntityPropertyComparer for complex objects

    What you're suggesting is interesting, but could lead to some potentially incorrect comparisons, say between an object that has been deeploaded, and one that hasn't. I'd rather see an parallel method, similar to the deepload/deepsave methods, i.e. it would allow you to specify what subsidiary objects to also compare.
    • Post Points: 5
Page 1 of 1 (2 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems