/// <summary>
        /// The learn.
        /// </summary>
        /// <param name="source">
        /// The source.
        /// </param>
        /// <param name="target">
        /// The target.
        /// </param>
        /// <returns>
        /// The <see cref="Path"/>.
        /// </returns>
        private Path Learn(object source, object target)
        {
            Path path                = null;
            var  sourceProps         = source.GetProps();
            var  targetProps         = target.GetProps();
            var  smartConventionInfo = new SmartConventionInfo {
                SourceType = source.GetType(), TargetType = target.GetType()
            };

            for (var i = 0; i < sourceProps.Count; i++)
            {
                var sourceProp = sourceProps[i];
                smartConventionInfo.SourceProp = sourceProp;

                var propFound = _propertyDict.ContainsKey(sourceProp.Name);

                // If the source prop's name is not in the allowed list, and is neither a value type nor a string, we need to check whether we should recurse or not
                if (!propFound && !sourceProp.PropertyType.IsValueType && sourceProp.PropertyType != typeof(string))
                {
                    // If source property is a Class ( and thus a navigational property ) and it was not found in the dictionary: Skip it
                    if (sourceProp.PropertyType.IsClass)
                    {
                        continue;
                    }

                    if (sourceProp.PropertyType.IsGenericType && sourceProp.PropertyType.GetGenericArguments()[0].IsClass)
                    {
                        continue;
                    }
                }

                for (var j = 0; j < targetProps.Count; j++)
                {
                    var targetProp = targetProps[j];
                    smartConventionInfo.TargetProp = targetProp;

                    if (!NameMatch(smartConventionInfo))
                    {
                        continue;
                    }

                    if (path == null)
                    {
                        path = new Path {
                            MatchingProps = new Dictionary <string, string> {
                                { smartConventionInfo.SourceProp.Name, smartConventionInfo.TargetProp.Name }
                            }
                        };
                    }
                    else
                    {
                        path.MatchingProps.Add(smartConventionInfo.SourceProp.Name, smartConventionInfo.TargetProp.Name);
                    }
                }
            }

            return(path);
        }
Пример #2
0
        /// <summary>
        /// The learn.
        /// </summary>
        /// <param name="source">
        /// The source.
        /// </param>
        /// <param name="target">
        /// The target.
        /// </param>
        /// <returns>
        /// The <see cref="Path"/>.
        /// </returns>
        private Path Learn(object source, object target)
        {
            Path path                = null;
            var  sourceProps         = source.GetProps();
            var  targetProps         = target.GetProps();
            var  smartConventionInfo = new SmartConventionInfo {
                SourceType = source.GetType(), TargetType = target.GetType()
            };

            for (var i = 0; i < sourceProps.Count; i++)
            {
                var sourceProp = sourceProps[i];
                smartConventionInfo.SourceProp = sourceProp;

                for (var j = 0; j < targetProps.Count; j++)
                {
                    var targetProp = targetProps[j];
                    smartConventionInfo.TargetProp = targetProp;

                    if (_maxDepth > 0)
                    {
                        if (!NameMatch(smartConventionInfo))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (!Match(smartConventionInfo))
                        {
                            continue;
                        }
                    }

                    if (path == null)
                    {
                        path = new Path {
                            MatchingProps = new Dictionary <string, string> {
                                { smartConventionInfo.SourceProp.Name, smartConventionInfo.TargetProp.Name }
                            }
                        };
                    }
                    else
                    {
                        path.MatchingProps.Add(smartConventionInfo.SourceProp.Name, smartConventionInfo.TargetProp.Name);
                    }
                }
            }

            return(path);
        }
 /// <summary>
 /// The name match.
 /// </summary>
 /// <param name="c">
 /// The c.
 /// </param>
 /// <returns>
 /// The <see cref="bool"/>.
 /// </returns>
 protected bool NameMatch(SmartConventionInfo c)
 {
     return(c.SourceProp.Name == c.TargetProp.Name);
 }
Пример #4
0
        /// <summary>
        /// The inject.
        /// </summary>
        /// <param name="source">
        /// The source.
        /// </param>
        /// <param name="target">
        /// The target.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        public object Inject(object source, object target)
        {
            Path path                = null;
            var  sourceProps         = source.GetProps();
            var  targetProps         = target.GetProps();
            var  smartConventionInfo = new SmartConventionInfo {
                SourceType = source.GetType(), TargetType = target.GetType()
            };

            for (var i = 0; i < sourceProps.Count; i++)
            {
                var sourceProp = sourceProps[i];
                smartConventionInfo.SourceProp = sourceProp;
                var sourceValue = GetValue(smartConventionInfo.SourceProp, source);
                if (sourceValue == null)
                {
                    continue;
                }

                for (var j = 0; j < targetProps.Count; j++)
                {
                    var targetProp = targetProps[j];
                    smartConventionInfo.TargetProp = targetProp;
                    var targetValue = GetValue(smartConventionInfo.TargetProp, target);

                    if (sourceValue.Equals(targetValue) || !Match(smartConventionInfo))
                    {
                        continue;
                    }

                    if (path == null)
                    {
                        path = new Path {
                            MatchingProps = new Dictionary <string, string> {
                                { smartConventionInfo.SourceProp.Name, smartConventionInfo.TargetProp.Name }
                            }
                        };
                    }
                    else
                    {
                        path.MatchingProps.Add(smartConventionInfo.SourceProp.Name, smartConventionInfo.TargetProp.Name);
                    }
                }
            }

            if (path != null)
            {
                foreach (var pair in path.MatchingProps)
                {
                    var sourceProp = sourceProps.GetByName(pair.Key);
                    var targetProp = targetProps.GetByName(pair.Value);
                    ExecuteMatch(new SmartMatchInfo {
                        Source = source, Target = target, SourceProp = sourceProp, TargetProp = targetProp
                    });
                }
            }

            dynamic composite = _compositeObject;

            return(composite);
        }
Пример #5
0
 /// <summary>
 /// The match.
 /// </summary>
 /// <param name="c">
 /// The c.
 /// </param>
 /// <returns>
 /// The <see cref="bool"/>.
 /// </returns>
 protected virtual bool Match(SmartConventionInfo c)
 {
     return(c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.PropertyType == c.TargetProp.PropertyType);
 }
 /// <summary>
 /// Determines if 2 properties match.
 ///     Match is determined by name and type.
 ///     The type check is lenient towards comparing base types to nullable types.
 /// </summary>
 /// <param name="c">
 /// The c.
 /// </param>
 /// <returns>
 /// The <see cref="bool"/>.
 /// </returns>
 protected virtual bool Match(SmartConventionInfo c)
 {
     return(c.SourceProp.Name == c.TargetProp.Name &&
            (c.SourceProp.PropertyType == c.TargetProp.PropertyType || Nullable.GetUnderlyingType(c.SourceProp.PropertyType) == c.TargetProp.PropertyType ||
             c.SourceProp.PropertyType == Nullable.GetUnderlyingType(c.TargetProp.PropertyType)));
 }