private ChangeData CreateChangeData(DescriptorData descriptorData, IDictionary <object, IEnumerable <NamedValue> > propertyValuesBeforeSave)
        {
            var entityDescriptorData = descriptorData as EntityDescriptorData;

            if (entityDescriptorData != null)
            {
                IEnumerable <NamedValue> propertyValues;
                ExceptionUtilities.Assert(propertyValuesBeforeSave.TryGetValue(entityDescriptorData.Entity, out propertyValues), "Could not find property values for descriptor: {0}", entityDescriptorData);

                return(EntityChangeData.Create(entityDescriptorData, propertyValues));
            }

            var linkDescriptorData = descriptorData as LinkDescriptorData;

            ExceptionUtilities.CheckObjectNotNull(linkDescriptorData, "Descriptor was neither an entity nor a link");

            return(LinkChangeData.Create(linkDescriptorData));
        }
            /// <summary>
            /// Creates LinkChangeData which captures the state of the specified link descriptor data.
            /// </summary>
            /// <param name="linkData">The link descriptor data.</param>
            /// <returns>Link change data.</returns>
            public static LinkChangeData Create(LinkDescriptorData linkData)
            {
                ExceptionUtilities.CheckArgumentNotNull(linkData, "linkData");

                LinkChangeData changeData = new LinkChangeData(linkData);

                var isEnumerable = linkData.SourceDescriptor.EntityClrType.GetProperty(linkData.SourcePropertyName).PropertyType.GetInterfaces().Any(t => t.Name == "IEnumerable");

                changeData.IsReference = !isEnumerable;

                if (linkData.TargetDescriptor != null)
                {
                    changeData.ClrTypeForRequery = linkData.TargetDescriptor.EntityClrType;
                }
                else
                {
                    ExceptionUtilities.Assert(changeData.IsReference, "TargetDescriptor can not be null for the link descriptor representing collection.");
                    changeData.ClrTypeForRequery = linkData.SourceDescriptor.EntityClrType.GetProperty(linkData.SourcePropertyName).PropertyType;
                }

                return(changeData);
            }
            /// <summary>
            /// Creates LinkChangeData which captures the state of the specified link descriptor data.
            /// </summary>
            /// <param name="linkData">The link descriptor data.</param>
            /// <returns>Link change data.</returns>
            public static LinkChangeData Create(LinkDescriptorData linkData)
            {
                ExceptionUtilities.CheckArgumentNotNull(linkData, "linkData");

                LinkChangeData changeData = new LinkChangeData(linkData);

                var isEnumerable = linkData.SourceDescriptor.EntityClrType.GetProperty(linkData.SourcePropertyName).PropertyType.GetInterfaces().Any(t => t.Name == "IEnumerable");
                changeData.IsReference = !isEnumerable;

                if (linkData.TargetDescriptor != null)
                {
                    changeData.ClrTypeForRequery = linkData.TargetDescriptor.EntityClrType;
                }
                else
                {
                    ExceptionUtilities.Assert(changeData.IsReference, "TargetDescriptor can not be null for the link descriptor representing collection.");
                    changeData.ClrTypeForRequery = linkData.SourceDescriptor.EntityClrType.GetProperty(linkData.SourcePropertyName).PropertyType;
                }

                return changeData;
            }