Example #1
0
        private static Expression GetChildMapping(
            IObjectMappingData childMappingData,
            MappingValues mappingValues,
            int dataSourceIndex,
            ObjectMapperData declaredTypeMapperData)
        {
            var childMapperData = childMappingData.MapperData;

            if (childMapperData.TargetMemberEverRecurses())
            {
                var mapRecursionCall = GetMapRecursionCallFor(
                    childMappingData,
                    mappingValues.SourceValue,
                    dataSourceIndex,
                    declaredTypeMapperData);

                return(mapRecursionCall);
            }

            var inlineMappingBlock = GetInlineMappingBlock(
                childMappingData,
                MappingDataFactory.ForChildMethod,
                mappingValues,
                new[]
            {
                mappingValues.SourceValue,
                mappingValues.TargetValue,
                mappingValues.EnumerableIndex,
                Expression.Constant(childMapperData.TargetMember.RegistrationName),
                Expression.Constant(dataSourceIndex),
                declaredTypeMapperData.GetParentMappingDataObjectAccess()
            });

            return(inlineMappingBlock);
        }
Example #2
0
        private static Expression GetInlineMappingBlock(
            IObjectMappingData childMappingData,
            MethodInfo createMethod,
            MappingValues mappingValues,
            Expression[] createMethodCallArguments)
        {
            var childMapper = childMappingData.Mapper;

            if (childMapper.MappingExpression.NodeType != ExpressionType.Try)
            {
                return(childMapper.MappingExpression);
            }

            if (!childMapper.MapperData.Context.UsesMappingDataObject)
            {
                return(GetDirectAccessMapping(
                           childMappingData,
                           mappingValues,
                           createMethod,
                           createMethodCallArguments));
            }

            var createInlineMappingDataCall = GetCreateMappingDataCall(
                createMethod,
                childMapper.MapperData,
                createMethodCallArguments);

            var mappingBlock = UseLocalSourceValueVariable(
                childMapper.MapperData.MappingDataObject,
                createInlineMappingDataCall,
                childMapper.MappingExpression);

            return(mappingBlock);
        }
        public static Expression GetInlineMappingBlock(
            IObjectMappingData mappingData,
            MappingValues mappingValues,
            Expression createMappingDataCall)
        {
            var mapper = mappingData.GetOrCreateMapper();

            if (mapper == null)
            {
                return(Constants.EmptyExpression);
            }

            if (mapper.MapperData.Context.UsesMappingDataObject)
            {
                return(UseLocalSourceValueVariable(
                           mapper.MapperData.MappingDataObject,
                           createMappingDataCall,
                           mapper.MappingExpression));
            }

            return(GetDirectAccessMapping(
                       mapper.MappingLambda.Body,
                       mapper.MapperData,
                       mappingValues,
                       createMappingDataCall));
        }
        public static Expression ForElement(
            MappingValues mappingValues,
            ObjectMapperData elementMapperData)
        {
            if (UseAsConversion(elementMapperData, out var asConversion))
            {
                return(asConversion);
            }

            var createMethod = MappingDataFactory
                               .ForElementMethod
                               .MakeGenericMethod(mappingValues.SourceValue.Type, mappingValues.TargetValue.Type);

            var enumerableMappingDataObject = elementMapperData.Context.IsStandalone
                ? typeof(IObjectMappingData).ToDefaultExpression()
                : (Expression)elementMapperData.Parent.MappingDataObject;

            var createCall = Expression.Call(
                createMethod,
                mappingValues.SourceValue,
                mappingValues.TargetValue,
                mappingValues.ElementIndex,
                mappingValues.ElementKey.GetConversionToObject(),
                enumerableMappingDataObject);

            return(createCall);
        }
Example #5
0
        public static Expression GetChildMapping(
            IQualifiedMember sourceMember,
            Expression sourceMemberAccess,
            int dataSourceIndex,
            IChildMemberMappingData childMappingData)
        {
            var childMapperData    = childMappingData.MapperData;
            var targetMemberAccess = childMapperData.GetTargetMemberAccess();

            var mappingValues = new MappingValues(
                sourceMemberAccess,
                targetMemberAccess,
                childMapperData.EnumerableIndex);

            var childObjectMappingData = ObjectMappingDataFactory.ForChild(
                sourceMember,
                childMapperData.TargetMember,
                dataSourceIndex,
                childMappingData.Parent);

            if (childObjectMappingData.MapperKey.MappingTypes.RuntimeTypesNeeded)
            {
                return(childMapperData.Parent.GetMapCall(
                           mappingValues.SourceValue,
                           childMapperData.TargetMember,
                           dataSourceIndex));
            }

            return(GetChildMapping(
                       childObjectMappingData,
                       mappingValues,
                       dataSourceIndex,
                       childMapperData.Parent));
        }
Example #6
0
        public static Expression ForChild(
            MappingValues mappingValues,
            int dataSourceIndex,
            ObjectMapperData childMapperData)
        {
            if (UseAsConversion(childMapperData, out var asConversion))
            {
                return(asConversion);
            }

            var createMethod = MappingDataFactory
                               .ForChildMethod
                               .MakeGenericMethod(childMapperData.SourceType, childMapperData.TargetType);

            var createCall = Expression.Call(
                createMethod,
                mappingValues.SourceValue,
                mappingValues.TargetValue,
                mappingValues.EnumerableIndex,
                childMapperData.TargetMember.RegistrationName.ToConstantExpression(),
                dataSourceIndex.ToConstantExpression(),
                childMapperData.Parent.MappingDataObject);

            return(createCall);
        }
Example #7
0
        public static Expression GetInlineMappingBlock(
            IObjectMappingData mappingData,
            MappingValues mappingValues,
            Expression createMappingDataCall)
        {
            var mapper = mappingData.GetOrCreateMapper();

            if (mapper == null)
            {
                if (mappingData.HasSameTypedConfiguredDataSource())
                {
                    // Configured data source for an otherwise-unconstructable complex type:
                    return(mappingValues.SourceValue);
                }

                return(Constants.EmptyExpression);
            }

            if (mapper.MapperData.Context.UsesMappingDataObject)
            {
                return(UseLocalValueVariable(
                           mapper.MapperData.MappingDataObject,
                           createMappingDataCall,
                           mapper.MappingExpression));
            }

            return(GetDirectAccessMapping(
                       mapper.MappingLambda.Body,
                       mapper.MapperData,
                       mappingValues,
                       createMappingDataCall));
        }
Example #8
0
        public static Expression GetChildMapping(
            IObjectMappingData childMappingData,
            MappingValues mappingValues,
            int dataSourceIndex,
            ObjectMapperData declaredTypeMapperData)
        {
            var childMapperData = childMappingData.MapperData;

            if (childMapperData.IsRepeatMapping &&
                childMapperData.RuleSet.RepeatMappingStrategy.AppliesTo(childMapperData))
            {
                var repeatMappingCall = childMapperData
                                        .RuleSet
                                        .RepeatMappingStrategy
                                        .GetMapRepeatedCallFor(
                    childMappingData,
                    mappingValues,
                    dataSourceIndex,
                    declaredTypeMapperData);

                return(repeatMappingCall);
            }

            var inlineMappingBlock = GetInlineMappingBlock(
                childMappingData,
                mappingValues,
                MappingDataCreationFactory.ForChild(mappingValues, dataSourceIndex, childMapperData));

            return(inlineMappingBlock);
        }
Example #9
0
        public static Expression GetElementMapping(
            IObjectMappingData elementMappingData,
            Expression sourceElementValue,
            Expression targetElementValue)
        {
            var enumerableMapperData = elementMappingData.Parent.MapperData;
            var elementMapperData    = elementMappingData.MapperData;

            Expression elementIndex, elementKey;

            if (elementMapperData.Context.IsStandalone)
            {
                elementIndex = elementMapperData.ElementIndex.GetNullableValueAccess();
                elementKey   = elementMapperData.ElementKey;
            }
            else
            {
                elementIndex = enumerableMapperData.EnumerablePopulationBuilder.Counter;
                elementKey   = enumerableMapperData.EnumerablePopulationBuilder.GetElementKey();
            }

            var mappingValues = new MappingValues(
                sourceElementValue,
                targetElementValue,
                elementIndex,
                elementKey,
                enumerableMapperData.DataSourceIndex);

            elementMapperData.Context.IsForNewElement =
                (targetElementValue.NodeType == ExpressionType.Default) ||
                (elementMapperData.DeclaredTypeMapperData?.Context.IsForNewElement == true);

            if (elementMapperData.IsRepeatMapping &&
                elementMapperData.RuleSet.RepeatMappingStrategy.AppliesTo(elementMapperData))
            {
                var repeatMappingCall = elementMapperData
                                        .RuleSet
                                        .RepeatMappingStrategy
                                        .GetMapRepeatedCallFor(
                    elementMappingData,
                    mappingValues,
                    enumerableMapperData);

                return(repeatMappingCall);
            }

            var inlineMappingBlock = GetInlineMappingBlock(
                elementMappingData,
                mappingValues,
                MappingDataCreationFactory.ForElement);

            return(inlineMappingBlock);
        }
Example #10
0
        public static Expression GetElementMapping(
            IObjectMappingData elementMappingData,
            Expression sourceElementValue,
            Expression targetElementValue)
        {
            var enumerableMapperData = elementMappingData.Parent.MapperData;
            var elementMapperData    = elementMappingData.MapperData;

            Expression enumerableIndex, parentMappingDataObject;

            if (elementMapperData.Context.IsStandalone)
            {
                enumerableIndex         = Expression.Property(elementMapperData.EnumerableIndex, "Value");
                parentMappingDataObject = typeof(IObjectMappingData).ToDefaultExpression();
            }
            else
            {
                enumerableIndex         = enumerableMapperData.EnumerablePopulationBuilder.Counter;
                parentMappingDataObject = enumerableMapperData.MappingDataObject;
            }

            var mappingValues = new MappingValues(
                sourceElementValue,
                targetElementValue,
                enumerableIndex);

            elementMapperData.Context.IsForNewElement = targetElementValue.NodeType == ExpressionType.Default;

            if (elementMapperData.IsRepeatMapping &&
                elementMapperData.RuleSet.RepeatMappingStrategy.AppliesTo(elementMapperData))
            {
                var repeatMappingCall = elementMapperData
                                        .RuleSet
                                        .RepeatMappingStrategy
                                        .GetMapRepeatedCallFor(
                    elementMappingData,
                    mappingValues,
                    enumerableMapperData.DataSourceIndex,
                    enumerableMapperData);

                return(repeatMappingCall);
            }

            var inlineMappingBlock = GetInlineMappingBlock(
                elementMappingData,
                mappingValues,
                MappingDataCreationFactory.ForElement(mappingValues, parentMappingDataObject, elementMapperData));

            return(inlineMappingBlock);
        }
Example #11
0
        private static Expression GetDirectAccessMapping(
            IObjectMappingData mappingData,
            MappingValues mappingValues,
            MethodInfo createMethod,
            Expression[] createMethodCallArguments)
        {
            var mapperData = mappingData.MapperData;
            var mapping    = mappingData.Mapper.MappingLambda.Body;

            var useLocalSourceValueVariable = UseLocalSourceValueVariable(mappingValues.SourceValue, mapping, mapperData);

            Expression sourceValue, sourceValueVariableValue = null;

            if (useLocalSourceValueVariable)
            {
                var sourceValueVariableName = GetSourceValueVariableName(mapperData, mappingValues.SourceValue.Type);
                sourceValue = Expression.Variable(mappingValues.SourceValue.Type, sourceValueVariableName);
                sourceValueVariableValue = mappingValues.SourceValue;
            }
            else
            {
                sourceValue = mappingValues.SourceValue;
            }

            var replacementsByTarget = new ExpressionReplacementDictionary
            {
                [mapperData.SourceObject]    = sourceValue,
                [mapperData.TargetObject]    = mappingValues.TargetValue,
                [mapperData.EnumerableIndex] = mappingValues.EnumerableIndex.GetConversionTo(mapperData.EnumerableIndex.Type)
            };

            var directAccessMapping = mapping.Replace(replacementsByTarget);

            var createInlineMappingDataCall = GetCreateMappingDataCall(
                createMethod,
                mapperData,
                createMethodCallArguments);

            directAccessMapping = directAccessMapping.Replace(
                mapperData.MappingDataObject,
                createInlineMappingDataCall);

            return(useLocalSourceValueVariable
                ? UseLocalSourceValueVariable((ParameterExpression)sourceValue, sourceValueVariableValue, directAccessMapping)
                : directAccessMapping);
        }
        private static Expression GetDerivedTypeRootMapping(
            IObjectMappingData derivedTypeMappingData,
            Expression sourceValue,
            Expression targetValue)
        {
            var mappingValues = new MappingValues(
                sourceValue,
                targetValue,
                typeof(int?).ToDefaultExpression());

            var inlineMappingBlock = MappingFactory.GetInlineMappingBlock(
                derivedTypeMappingData,
                mappingValues,
                MappingDataCreationFactory.ForDerivedType(derivedTypeMappingData.MapperData));

            return(inlineMappingBlock);
        }
Example #13
0
        public static Expression GetDirectAccessMapping(
            Expression mapping,
            IMemberMapperData mapperData,
            MappingValues mappingValues,
            Expression createMappingDataCall)
        {
            var useLocalSourceValueVariable =
                ShouldUseLocalSourceValueVariable(mappingValues.SourceValue, mapping, mapperData);

            Expression sourceValue, sourceValueVariableValue;

            if (useLocalSourceValueVariable)
            {
                var sourceValueVariableName = mappingValues.SourceValue.Type.GetSourceValueVariableName();
                sourceValue = Expression.Variable(mappingValues.SourceValue.Type, sourceValueVariableName);
                sourceValueVariableValue = mappingValues.SourceValue;
            }
            else
            {
                sourceValue = mappingValues.SourceValue;
                sourceValueVariableValue = null;
            }

            var replacementsByTarget = FixedSizeExpressionReplacementDictionary
                                       .WithEquivalentKeys(4)
                                       .Add(mapperData.SourceObject, sourceValue)
                                       .Add(mapperData.TargetObject, mappingValues.TargetValue)
                                       .Add(mapperData.ElementKey, mappingValues.ElementKey)
                                       .Add(
                mapperData.ElementIndex,
                mappingValues.ElementIndex.GetConversionTo(mapperData.ElementIndex.Type));

            mapping = mapping
                      .Replace(replacementsByTarget)
                      .Replace(mapperData.MappingDataObject, createMappingDataCall);

            return(useLocalSourceValueVariable
                ? UseLocalValueVariable(
                       (ParameterExpression)sourceValue,
                       sourceValueVariableValue,
                       mapping,
                       mapperData)
                : mapping);
        }
Example #14
0
        private static Expression GetDerivedTypeChildMapping(
            IObjectMappingData derivedTypeMappingData,
            Expression sourceValue,
            Expression targetValue)
        {
            var derivedTypeMapperData  = derivedTypeMappingData.MapperData;
            var declaredTypeMapperData = derivedTypeMappingData.DeclaredTypeMappingData.MapperData;

            var mappingValues = new MappingValues(
                sourceValue,
                targetValue,
                derivedTypeMapperData.EnumerableIndex);

            return(GetChildMapping(
                       derivedTypeMappingData,
                       mappingValues,
                       declaredTypeMapperData.DataSourceIndex,
                       declaredTypeMapperData));
        }
Example #15
0
        private static Expression GetDirectAccessMapping(
            Expression mapping,
            IMemberMapperData mapperData,
            MappingValues mappingValues,
            Expression createMappingDataCall)
        {
            var useLocalSourceValueVariable =
                ShouldUseLocalSourceValueVariable(mappingValues.SourceValue, mapping, mapperData);

            Expression sourceValue, sourceValueVariableValue;

            if (useLocalSourceValueVariable)
            {
                var sourceValueVariableName = mappingValues.SourceValue.Type.GetSourceValueVariableName();
                sourceValue = Expression.Variable(mappingValues.SourceValue.Type, sourceValueVariableName);
                sourceValueVariableValue = mappingValues.SourceValue;
            }
            else
            {
                sourceValue = mappingValues.SourceValue;
                sourceValueVariableValue = null;
            }

            var replacementsByTarget = new ExpressionReplacementDictionary(3)
            {
                [mapperData.SourceObject]    = sourceValue,
                [mapperData.TargetObject]    = mappingValues.TargetValue,
                [mapperData.EnumerableIndex] = mappingValues.EnumerableIndex.GetConversionTo(mapperData.EnumerableIndex.Type)
            };

            mapping = mapping
                      .Replace(replacementsByTarget)
                      .Replace(mapperData.MappingDataObject, createMappingDataCall);

            return(useLocalSourceValueVariable
                ? UseLocalValueVariable(
                       (ParameterExpression)sourceValue,
                       sourceValueVariableValue,
                       mapping,
                       mapperData)
                : mapping);
        }
Example #16
0
        private static Expression GetDerivedTypeRootMapping(
            IObjectMappingData derivedTypeMappingData,
            Expression sourceValue,
            Expression targetValue)
        {
            var mappingValues = new MappingValues(
                sourceValue,
                targetValue,
                Expression.Default(typeof(int?)));

            // Derived type conversions are performed with ObjectMappingData.As<TDerivedSource, TDerivedTarget>()
            // so no need for createMethod or createMethodCallArguments arguments:
            var inlineMappingBlock = GetInlineMappingBlock(
                derivedTypeMappingData,
                null,
                mappingValues,
                null);

            return(inlineMappingBlock);
        }
Example #17
0
        private static Expression GetInlineElementMappingBlock(
            IObjectMappingData elementMappingData,
            Expression sourceElementValue,
            Expression targetElementValue)
        {
            var enumerableMapperData = elementMappingData.Parent.MapperData;
            var elementMapperData    = elementMappingData.MapperData;

            Expression enumerableIndex, parentMappingDataObject;

            if (elementMapperData.Context.IsStandalone)
            {
                enumerableIndex         = Expression.Property(elementMapperData.EnumerableIndex, "Value");
                parentMappingDataObject = Expression.Default(typeof(IObjectMappingData));
            }
            else
            {
                enumerableIndex         = enumerableMapperData.EnumerablePopulationBuilder.Counter;
                parentMappingDataObject = enumerableMapperData.MappingDataObject;
            }

            var mappingValues = new MappingValues(
                sourceElementValue,
                targetElementValue,
                enumerableIndex);

            elementMapperData.Context.IsForNewElement = targetElementValue.NodeType == ExpressionType.Default;

            return(GetInlineMappingBlock(
                       elementMappingData,
                       MappingDataFactory.ForElementMethod,
                       mappingValues,
                       new[]
            {
                mappingValues.SourceValue,
                mappingValues.TargetValue,
                mappingValues.EnumerableIndex,
                parentMappingDataObject
            }));
        }
Example #18
0
        public static Expression ForElement(
            MappingValues mappingValues,
            Expression enumerableMappingDataObject,
            ObjectMapperData childMapperData)
        {
            if (UseAsConversion(childMapperData, out var asConversion))
            {
                return(asConversion);
            }

            var createMethod = MappingDataFactory
                               .ForElementMethod
                               .MakeGenericMethod(mappingValues.SourceValue.Type, mappingValues.TargetValue.Type);

            var createCall = Expression.Call(
                createMethod,
                mappingValues.SourceValue,
                mappingValues.TargetValue,
                mappingValues.EnumerableIndex,
                enumerableMappingDataObject);

            return(createCall);
        }
 public static Expression ForDerivedType(MappingValues mappingValues, ObjectMapperData childMapperData)
 {
     UseAsConversion(childMapperData, out var asConversion);
     return(asConversion);
 }