private static BlockExpression GetLoopBody(
            IPopulationLoopData loopData,
            EnumerablePopulationBuilder builder,
            Expression breakLoop,
            Expression elementPopulation)
        {
            var ifExitCheckBreakLoop = Expression.IfThen(loopData.LoopExitCheck, breakLoop);
            var counterIncrement     = builder.GetCounterIncrement();

            elementPopulation = ApplySourceFilterIfAppropriate(elementPopulation, loopData, builder);

            if (elementPopulation.NodeType != ExpressionType.Block)
            {
                return(Expression.Block(ifExitCheckBreakLoop, elementPopulation, counterIncrement));
            }

            var elementPopulationBlock = (BlockExpression)elementPopulation;

            var loopExpressions = new Expression[elementPopulationBlock.Expressions.Count + 2];

            loopExpressions[0] = ifExitCheckBreakLoop;
            loopExpressions.CopyFrom(elementPopulationBlock.Expressions, startIndex: 1);
            loopExpressions[loopExpressions.Length - 1] = counterIncrement;

            return(elementPopulationBlock.Variables.Any()
                ? Expression.Block(elementPopulationBlock.Variables, loopExpressions)
                : Expression.Block(loopExpressions));
        }
        private static Expression ApplySourceFilterIfAppropriate(
            Expression elementPopulation,
            IPopulationLoopData loopData,
            EnumerablePopulationBuilder builder)
        {
            if (!builder.MapperData.MapperContext.UserConfigurations.HasSourceValueFilters)
            {
                return(elementPopulation);
            }

            var sourceElement = loopData.GetSourceElementValue();

            var sourceValueFilters = builder.MapperData
                                     .GetSourceValueFilters(sourceElement.Type);

            if (sourceValueFilters.None())
            {
                return(elementPopulation);
            }

            var sourceFilterConditions = sourceValueFilters
                                         .GetFilterConditionsOrNull(sourceElement, builder.MapperData);

            return((sourceFilterConditions != null)
                ? Expression.IfThen(sourceFilterConditions, elementPopulation)
                : elementPopulation);
        }
        private void InsertSourceElementNullCheck(
            IPopulationLoopData loopData,
            DictionaryTargetMember dictionaryEntryMember,
            IMemberMapperData mapperData,
            IList <Expression> mappingExpressions)
        {
            var sourceElement = loopData.GetSourceElementValue();

            if (sourceElement.Type.CannotBeNull())
            {
                return;
            }

            loopData.NeedsContinueTarget = true;

            var sourceElementIsNull = sourceElement.GetIsDefaultComparison();

            var nullTargetValue = dictionaryEntryMember.ValueType.ToDefaultExpression();
            var addNullEntry    = dictionaryEntryMember.GetPopulation(nullTargetValue, mapperData);

            var incrementCounter = _wrappedBuilder.GetCounterIncrement();
            var continueLoop     = Expression.Continue(loopData.ContinueLoopTarget);

            var nullEntryActions = Expression.Block(addNullEntry, incrementCounter, continueLoop);

            var ifNullContinue = Expression.IfThen(sourceElementIsNull, nullEntryActions);

            mappingExpressions.Insert(0, ifNullContinue);
        }
        private void AddDerivedSourceTypePopulations(
            IPopulationLoopData loopData,
            QualifiedMember dictionaryEntryMember,
            IObjectMappingData mappingData,
            IEnumerable <Type> derivedSourceTypes,
            ICollection <ParameterExpression> typedVariables,
            ICollection <Expression> mappingExpressions)
        {
            var sourceElement  = loopData.GetSourceElementValue();
            var mapNextElement = Expression.Continue(loopData.ContinueLoopTarget);

            var orderedDerivedSourceTypes = derivedSourceTypes
                                            .OrderBy(t => t, TypeComparer.MostToLeastDerived);

            foreach (var derivedSourceType in orderedDerivedSourceTypes)
            {
                var derivedSourceCheck      = new DerivedSourceTypeCheck(derivedSourceType);
                var typedVariableAssignment = derivedSourceCheck.GetTypedVariableAssignment(sourceElement);

                typedVariables.Add(derivedSourceCheck.TypedVariable);
                mappingExpressions.Add(typedVariableAssignment);

                var derivedTypeMapping    = GetDerivedTypeMapping(derivedSourceCheck, mappingData);
                var derivedTypePopulation = GetPopulation(derivedTypeMapping, dictionaryEntryMember, mappingData);
                var incrementCounter      = _wrappedBuilder.GetCounterIncrement();
                var derivedMappingBlock   = Expression.Block(derivedTypePopulation, incrementCounter, mapNextElement);
                var ifDerivedTypeReturn   = Expression.IfThen(derivedSourceCheck.TypeCheck, derivedMappingBlock);

                mappingExpressions.Add(ifDerivedTypeReturn);
            }
        }
        private Expression AssignDictionaryEntry(IPopulationLoopData loopData, IObjectMappingData mappingData)
        {
            loopData.NeedsContinueTarget = true;

            var dictionaryEntryMember = _targetDictionaryMember.GetElementMember();

            return(AssignDictionaryEntry(loopData, dictionaryEntryMember, mappingData));
        }
Esempio n. 6
0
        private Expression GetPopulation(
            IPopulationLoopData loopData,
            QualifiedMember dictionaryEntryMember,
            IObjectMappingData dictionaryMappingData)
        {
            var elementMapping = loopData.GetElementMapping(dictionaryMappingData);

            return(GetPopulation(elementMapping, dictionaryEntryMember, dictionaryMappingData));
        }
        private Expression AssignDictionaryEntry(
            IPopulationLoopData loopData,
            DictionaryTargetMember dictionaryEntryMember,
            IObjectMappingData mappingData)
        {
            if (_wrappedBuilder.ElementTypesAreSimple)
            {
                return(GetPopulation(loopData, dictionaryEntryMember, mappingData));
            }

            mappingData = GetMappingData(mappingData);

            if (dictionaryEntryMember.HasComplexEntries)
            {
                return(GetPopulation(loopData, dictionaryEntryMember, mappingData));
            }

            List <ParameterExpression> typedVariables;
            List <Expression>          mappingExpressions;

            var derivedSourceTypes    = mappingData.MapperData.GetDerivedSourceTypes();
            var hasDerivedSourceTypes = derivedSourceTypes.Any();

            if (hasDerivedSourceTypes)
            {
                typedVariables     = new List <ParameterExpression>(derivedSourceTypes.Count);
                mappingExpressions = new List <Expression>(typedVariables.Count * 2 + 2);

                AddDerivedSourceTypePopulations(
                    loopData,
                    dictionaryEntryMember,
                    mappingData,
                    derivedSourceTypes,
                    typedVariables,
                    mappingExpressions);
            }
            else
            {
                typedVariables     = null;
                mappingExpressions = new List <Expression>(2);
            }

            mappingExpressions.Add(GetPopulation(loopData, dictionaryEntryMember, mappingData));

            InsertSourceElementNullCheck(
                loopData,
                dictionaryEntryMember,
                mappingData.MapperData,
                mappingExpressions);

            var mappingBlock = hasDerivedSourceTypes
                ? Expression.Block(typedVariables, mappingExpressions)
                : Expression.Block(mappingExpressions);

            return(mappingBlock);
        }
Esempio n. 8
0
        private static void InsertSourceElementNullCheck(IPopulationLoopData loopData, IList <Expression> mappingExpressions)
        {
            var sourceElement = loopData.GetSourceElementValue();

            if (sourceElement.Type.CannotBeNull())
            {
                return;
            }

            loopData.NeedsContinueTarget = true;

            var sourceElementIsNull = sourceElement.GetIsDefaultComparison();
            var continueLoop        = Expression.Continue(loopData.ContinueLoopTarget);
            var ifNullContinue      = Expression.IfThen(sourceElementIsNull, continueLoop);

            mappingExpressions.Insert(0, ifNullContinue);
        }
        private Expression GetPopulation(
            IPopulationLoopData loopData,
            DictionaryTargetMember dictionaryEntryMember,
            IObjectMappingData dictionaryMappingData)
        {
            var elementMapping = loopData.GetElementMapping(dictionaryMappingData);

            if (dictionaryEntryMember.HasKey &&
                dictionaryEntryMember.CheckExistingElementValue &&
                dictionaryMappingData.MapperData.TargetCouldBePopulated())
            {
                elementMapping = elementMapping.Replace(
                    dictionaryMappingData.MapperData.GetTargetMemberDictionaryKey(),
                    dictionaryEntryMember.Key,
                    ExpressionEvaluation.Equivalator);
            }

            return(GetPopulation(elementMapping, dictionaryEntryMember, dictionaryMappingData));
        }