Exemple #1
0
        private IEnumerable <Expression> GetConfiguredToTargetDataSourceMappings(MappingCreationContext context)
        {
            if (!HasConfiguredToTargetDataSources(context.MapperData, out var configuredToTargetDataSources))
            {
                yield break;
            }

            for (var i = 0; i < configuredToTargetDataSources.Count;)
            {
                var configuredToTargetDataSource = configuredToTargetDataSources[i++];
                var newSourceContext             = context.WithDataSource(configuredToTargetDataSource);

                AddPopulationsAndCallbacks(newSourceContext);

                if (newSourceContext.MappingExpressions.None())
                {
                    continue;
                }

                context.UpdateFrom(newSourceContext);

                var mapping = newSourceContext.MappingExpressions.HasOne()
                    ? newSourceContext.MappingExpressions.First()
                    : Expression.Block(newSourceContext.MappingExpressions);

                mapping = MappingFactory.UseLocalToTargetDataSourceVariableIfAppropriate(
                    context.MapperData,
                    newSourceContext.MapperData,
                    configuredToTargetDataSource.Value,
                    mapping);

                if (!configuredToTargetDataSource.IsConditional)
                {
                    yield return(mapping);

                    continue;
                }

                if (context.MapperData.TargetMember.IsComplex || (i > 1))
                {
                    yield return(Expression.IfThen(configuredToTargetDataSource.Condition, mapping));

                    continue;
                }

                var fallback = context.MapperData.LocalVariable.Type.GetEmptyInstanceCreation(
                    context.TargetMember.ElementType,
                    context.MapperData.EnumerablePopulationBuilder.TargetTypeHelper);

                var assignFallback = context.MapperData.LocalVariable.AssignTo(fallback);

                yield return(Expression.IfThenElse(configuredToTargetDataSource.Condition, mapping, assignFallback));
            }
        }
Exemple #2
0
        private IEnumerable <Expression> GetConfiguredRootDataSourcePopulations(MappingCreationContext context)
        {
            if (!HasConfiguredRootDataSources(context.MapperData, out var configuredRootDataSources))
            {
                yield break;
            }

            for (var i = 0; i < configuredRootDataSources.Count; ++i)
            {
                var configuredRootDataSource = configuredRootDataSources[i];
                var newSourceContext         = context.WithDataSource(configuredRootDataSource);

                var memberPopulations = GetObjectPopulation(newSourceContext).WhereNotNull().ToArray();

                if (memberPopulations.None())
                {
                    continue;
                }

                context.UpdateFrom(newSourceContext);

                var mapping = memberPopulations.HasOne()
                    ? memberPopulations.First()
                    : Expression.Block(memberPopulations);

                if (!configuredRootDataSource.IsConditional)
                {
                    yield return(mapping);

                    continue;
                }

                if (context.MapperData.TargetMember.IsComplex || (i > 0))
                {
                    yield return(Expression.IfThen(configuredRootDataSource.Condition, mapping));

                    continue;
                }

                var fallback = context.MapperData.LocalVariable.Type.GetEmptyInstanceCreation(
                    context.TargetMember.ElementType,
                    context.MapperData.EnumerablePopulationBuilder.TargetTypeHelper);

                var assignFallback = context.MapperData.LocalVariable.AssignTo(fallback);

                yield return(Expression.IfThenElse(configuredRootDataSource.Condition, mapping, assignFallback));
            }
        }
        private Expression GetConfiguredToTargetDataSourceMappingOrNull(
            MappingCreationContext context,
            IConfiguredDataSource toTargetDataSource,
            bool isFirstDataSource)
        {
            if (context.MapperData.Context.IsForToTargetMapping)
            {
                return(null);
            }

            var toTargetContext = context.WithToTargetDataSource(toTargetDataSource);

            AddPopulationsAndCallbacks(toTargetContext);

            if (toTargetContext.MappingExpressions.None())
            {
                return(null);
            }

            context.UpdateFrom(toTargetContext, toTargetDataSource);

            var originalMapperData = context.MapperData;
            var isSequential       = toTargetDataSource.IsSequential;

            if (!isSequential)
            {
                toTargetContext.MappingExpressions.Add(
                    context.MapperData.GetReturnExpression(GetExpressionToReturn(toTargetContext)));
            }

            var toTargetMapping = MappingFactory.UseLocalToTargetDataSourceVariableIfAppropriate(
                originalMapperData,
                toTargetContext.MapperData,
                toTargetDataSource.Value,
                toTargetContext.GetMappingExpression());

            var hasCondition = isSequential
                ? toTargetDataSource.IsConditional
                : toTargetDataSource.HasConfiguredCondition;

            if (!hasCondition)
            {
                return(toTargetMapping);
            }

            Expression fallback;

            if (!isFirstDataSource || originalMapperData.TargetMember.IsComplex)
            {
                if (isSequential || !originalMapperData.TargetMemberIsEnumerableElement())
                {
                    return(Expression.IfThen(toTargetDataSource.Condition, toTargetMapping));
                }

                // Mapping a configured ToTargetInstead() data source to
                // a complex type enumerable element member; reset the
                // local instance variable to null to prevent reuse of a
                // previous element's mapping result:
                fallback = originalMapperData.GetTargetMemberDefault();
            }
            else
            {
                fallback = originalMapperData.LocalVariable.Type.GetEmptyInstanceCreation(
                    context.TargetMember.ElementType,
                    originalMapperData.EnumerablePopulationBuilder.TargetTypeHelper);
            }

            var assignFallback = originalMapperData.LocalVariable.AssignTo(fallback);

            return(Expression.IfThenElse(toTargetDataSource.Condition, toTargetMapping, assignFallback));
        }