Form comparison is failed

Dec 21, 2011 at 10:04 AM
Edited Dec 21, 2011 at 10:07 AM

I found that form comparison failed if you have 2 or more forms.

To test, just create some form of entity and try to compare. 

I suggest:

 

 

private bool BuildArrayComparisonTypes(CustomizationComparison parent, PropertyInfo prop, IEnumerable source, IEnumerable target)
        {
           ....

            // If the arrays need special sorting, this will sort them and 
            // insert nulls for the missing entries from either the source 
            // or target.
            if (!SynchronizeArraysByIdentity(ref sourceArray, ref targetArray))
            {
                if (parent.Name == "Form")
                {
                    SynchronizeArraysByFormId(ref sourceArray, ref targetArray);
                }
            }
            ....
        }

 

 

 

private static bool SynchronizeArraysByIdentity(ref Array source, ref Array target)
        {
            if (source == null && target == null) return false;
            
            // Only proceed if the array elements implement IIdentifiable.
            Type elementType = (source ?? target).GetType().GetElementType();
            if (!typeof(IIdentifiable).IsAssignableFrom(elementType)) return false;

            IIdentifiable[] sourceIdentities = source == null ? new IIdentifiable[0] :
                source.Cast<IIdentifiable>().ToArray();

            IIdentifiable[] targetIdentities = target == null ? new IIdentifiable[0] :
                target.Cast<IIdentifiable>().ToArray();

            // create a list of combined entities to determine the order by 
            // which both lists will be sorted
            string[] combinedIdentities = sourceIdentities.Select(i => i.Identity)
                .Union(targetIdentities.Select(i => i.Identity))
                .OrderBy(s => s)
                .ToArray();

            source = SortAndPad(combinedIdentities, sourceIdentities);
            target = SortAndPad(combinedIdentities, targetIdentities);
            return true;
        }

 

 

 

private static bool SynchronizeArraysByFormId(ref Array source, ref Array target)
        {
            if (source == null && target == null) return false;

            // Only proceed if the array elements implement IIdentifiable.
            Type elementType = (source ?? target).GetType().GetElementType();
            if (!typeof(SystemFormsTypeSystemform).IsAssignableFrom(elementType)) return false;

            SystemFormsTypeSystemform[] sourceIdentities = source == null ? new SystemFormsTypeSystemform[0] :
                source.Cast<SystemFormsTypeSystemform>().ToArray();

            SystemFormsTypeSystemform[] targetIdentities = target == null ? new SystemFormsTypeSystemform[0] :
                target.Cast<SystemFormsTypeSystemform>().ToArray();

            // create a list of combined entities to determine the order by 
            // which both lists will be sorted
            string[] combinedIdentities = sourceIdentities.Select(i => i.formid)
                .Union(targetIdentities.Select(i => i.formid))
                .OrderBy(s => s)
                .ToArray();

            source = SortAndPad(combinedIdentities, sourceIdentities);
            target = SortAndPad(combinedIdentities, targetIdentities);
            return true;
        }

private static Array SortAndPad(string[] identityKeys, SystemFormsTypeSystemform[] array)
        {
            if (array == null) return null;

            return identityKeys
                .Select(k => array.FirstOrDefault(i => i.formid == k))
                .ToArray();
        }

Dec 24, 2011 at 3:30 AM

Try it with the changes I just added.  I am using it with 4 forms per entity and I am not having any issues.