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)); } }
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)); } }