Beispiel #1
0
        /// <summary>
        /// Register a manual mapping information from source to target
        /// </summary>
        /// <typeparam name="TSource">Source type</typeparam>
        /// <typeparam name="TTarget">Target type</typeparam>
        public void RegisterMap <TSource, TTarget>(Action <TSource, TTarget> convert)
        {
            var targetType = typeof(TTarget);
            var sourceType = typeof(TSource);

            if (!_mappings.ContainsKey(targetType))
            {
                _mappings[targetType] = new Dictionary <Type, MappingInfo>();
            }
            if (!_mappings[targetType].ContainsKey(sourceType))
            {
                _mappings[targetType][sourceType] = new MappingInfo
                {
                    SourceType = sourceType,
                    TargetType = targetType
                }
            }
            ;

            _mappings[targetType][sourceType].ManualConvertAction = (source, target) =>
            {
                convert((TSource)source, (TTarget)target);
            };
        }
Beispiel #2
0
        /// <summary>
        /// Register mapping information from source to target
        /// </summary>
        /// <typeparam name="TSource">Source type</typeparam>
        /// <typeparam name="TTarget">Target type</typeparam>
        public void RegisterAutoMap <TSource, TTarget>(Action <TSource, TTarget> additionalMapping = null)
        {
            var targetType = typeof(TTarget);
            var sourceType = typeof(TSource);
            MappingInfo <TSource, TTarget> info = new MappingInfo <TSource, TTarget>
            {
                SourceType = sourceType,
                TargetType = targetType
            };


            var sourcePoperties = sourceType.GetTypeInfo().GetProperties();
            var targetPoperties = targetType.GetTypeInfo().GetProperties(e => e.SetMethod != null);

            List <PropertyMapper> props = new List <PropertyMapper>();

            foreach (var targetPoperty in targetPoperties)
            {
                bool sourceNullable = false;
                bool targetNullable = false;

                var sourceProperty =
                    sourcePoperties.FirstOrDefault(
                        e => e.Name == targetPoperty.Name &&
                        e.PropertyType == targetPoperty.PropertyType);
                if (sourceProperty == null &&
                    targetPoperty.PropertyType.GenericTypeArguments.Any() &&
                    targetPoperty.PropertyType == typeof(Nullable <>).MakeGenericType(targetPoperty.PropertyType.GenericTypeArguments))
                {
                    sourceProperty =
                        sourcePoperties.FirstOrDefault(e => e.Name == targetPoperty.Name &&
                                                       e.PropertyType == targetPoperty.PropertyType.GenericTypeArguments[0]);
                    targetNullable = true;
                }

                if (sourceProperty == null)
                {
                    sourceProperty =
                        sourcePoperties.FirstOrDefault(e => e.Name == targetPoperty.Name &&
                                                       e.PropertyType.GenericTypeArguments.Any() &&
                                                       e.PropertyType == typeof(Nullable <>).MakeGenericType(targetPoperty.PropertyType));
                    sourceNullable = true;
                }

                if (sourceProperty != null)
                {
                    props.Add(new PropertyMapper
                    {
                        TargetProperty = targetPoperty,
                        SourceProperty = sourceProperty,
                        SourceNullable = sourceNullable,
                        TargetNullable = targetNullable
                    });
                }
            }

            info.Properties = props;

            if (!_mappings.ContainsKey(targetType))
            {
                _mappings[targetType] = new Dictionary <Type, MappingInfo>();
            }
            if (_mappings[targetType].ContainsKey(sourceType))
            {
                info.ManualConvertAction = _mappings[targetType][sourceType].ManualConvertAction;
            }

            info.SetAdditionalMapping(additionalMapping);
            _mappings[targetType][sourceType] = info;
        }