Esempio n. 1
0
        public static void ApplyTransformation(ITransformable target, BaseModifications modifications, EntityFramework edmx, string[] versions, HandlePropertyModification alternativeHandler = null)
        {
            // For eacn property on the modifications instance, if there is a matching property on target
            // then we copy the value from modification to target, if the modification value is not null
            var modificationProperties = modifications.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
                                         .Where(x => x.DeclaringType != typeof(BaseModifications));
            var targetProperties = target.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).ToDictionary(x => x.Name, x => x);

            foreach (var modsProp in modificationProperties)
            {
                var modsValue = modsProp.GetValue(modifications);
                if (null == modsValue)
                {
                    continue;
                }

                if (null != alternativeHandler && alternativeHandler(modsProp.Name, modsValue))
                {
                    continue;
                }

                Type modificationType;
                if (modsProp.IsModificationDictionary(out modificationType))
                {
                    PropertyInfo targetProp;
                    if (targetProperties.TryGetValue(modsProp.Name, out targetProp))
                    {
                        var targetValue = targetProp.GetValue(target);
                        InvokeApplyTransformationToCollection((IDictionary)modsValue, (IList)targetValue, edmx, versions);
                    }
                    else
                    {
                        Console.WriteLine($"Failed to locate a target property named '{modsProp.Name}' on '{target.GetType().Name}'.");
                    }
                }
                else if (modsProp.IsSimpleType())
                {
                    PropertyInfo targetProp;
                    if (targetProperties.TryGetValue(modsProp.Name, out targetProp))
                    {
                        targetProp.SetValue(target, modsValue);
                    }
                    else
                    {
                        Console.WriteLine($"Failed to locate a target property named '{modsProp.Name}' on '{target.GetType().Name}'.");
                    }
                }
                else
                {
                    // See if the object we're pointing at can be transformed
                    PropertyInfo targetProp;
                    if (targetProperties.TryGetValue(modsProp.Name, out targetProp))
                    {
                        object targetPropertyValue = targetProp.GetValue(target);
                        if (null == targetPropertyValue)
                        {
                            // Create a new default instance of this class, since we're trying to set properties on it
                            targetPropertyValue = CreateNewInstanceOfType(targetProp.PropertyType);
                            // Store the newly created instance back on the target object
                            targetProp.SetValue(target, targetPropertyValue);
                        }

                        ITransformable transformableTargetProperty = targetPropertyValue as ITransformable;
                        if (null != transformableTargetProperty)
                        {
                            transformableTargetProperty.ApplyTransformation((BaseModifications)modsValue, edmx, versions);
                            continue;
                        }
                    }
                    //Unsupported
                    throw new NotSupportedException();
                }
            }
        }