public static Expression GetFallbackCollectionValue(this IMemberMapperData mapperData) { var targetMember = mapperData.TargetMember; Expression emptyEnumerable; if (mapperData.TargetMemberHasInitAccessibleValue()) { var existingValue = mapperData.GetTargetMemberAccess(); if (mapperData.MapToNullCollections()) { return(existingValue); } emptyEnumerable = targetMember.Type.GetEmptyInstanceCreation(targetMember.ElementType); return(Expression.Coalesce(existingValue, emptyEnumerable)); } if (mapperData.MapToNullCollections()) { return(targetMember.Type.ToDefaultExpression()); } emptyEnumerable = targetMember.Type.GetEmptyInstanceCreation(targetMember.ElementType); return(emptyEnumerable.GetConversionTo(targetMember.Type)); }
private static Expression GetValue(IMemberMapperData mapperData) { if (mapperData.TargetMember.IsReadable) { var existingValue = mapperData.GetTargetMemberAccess(); if (!mapperData.TargetMember.IsEnumerable) { return(existingValue); } var emptyEnumerable = mapperData.TargetMember.GetEmptyInstanceCreation(); return(Expression.Coalesce(existingValue, emptyEnumerable)); } if (mapperData.TargetMember.IsEnumerable) { var emptyEnumerable = mapperData.TargetMember.GetEmptyInstanceCreation(); return(emptyEnumerable.GetConversionTo(mapperData.TargetMember.Type)); } return(Expression.Default(mapperData.TargetMember.Type)); }
public PreserveExistingValueDataSource(IMemberMapperData mapperData) : this( mapperData.SourceMember, mapperData.TargetMember.IsReadable ? mapperData.GetTargetMemberAccess() : Constants.EmptyExpression) { }
private static Expression GetConversionOrCreationExpression( this IMemberMapperData mapperData, Expression value, Type targetType, IList <ConfiguredObjectFactory> valueFactories) { var simpleMemberMapperData = SimpleMemberMapperData.Create(value, mapperData); var checkNestedAccesses = simpleMemberMapperData.TargetMemberIsEnumerableElement() && value.Type.CanBeNull(); var replacements = FixedSizeExpressionReplacementDictionary .WithEquivalentKeys(3) .Add(simpleMemberMapperData.SourceObject, value) .Add(simpleMemberMapperData.TargetObject, mapperData.GetTargetMemberAccess()) .Add(simpleMemberMapperData.ElementIndex, simpleMemberMapperData.ElementIndexValue); var conversions = valueFactories.ProjectToArray(vf => { var factoryExpression = vf.Create(simpleMemberMapperData); var condition = vf.GetConditionOrNull(simpleMemberMapperData); if (checkNestedAccesses) { var nestedAccessChecks = NestedAccessChecksFactory .GetNestedAccessChecksFor(factoryExpression); if (nestedAccessChecks != null) { condition = condition != null ? Expression.AndAlso(nestedAccessChecks, condition) : nestedAccessChecks; } } return(new SimpleTypeValueFactory { Factory = factoryExpression.Replace(replacements).GetConversionTo(targetType), Condition = condition?.Replace(replacements) }); }); if (conversions.Last().Condition != null) { conversions = conversions.Append(new SimpleTypeValueFactory { Factory = mapperData.GetValueConversion(value, targetType) ?? simpleMemberMapperData.GetTargetMemberDefault() }); } else if (conversions.Length == 1) { return(conversions[0].Factory); } return(GetConversionExpression(conversions)); }
private static Expression GetValue(IMemberMapperData mapperData) { if (mapperData.TargetMember.IsEnumerable) { return(FallbackToCollection(mapperData) ? mapperData.GetFallbackCollectionValue() : mapperData.GetTargetMemberDefault()); } if (mapperData.TargetMember.IsReadable && !mapperData.UseMemberInitialisations()) { return(mapperData.GetTargetMemberAccess()); } return(mapperData.GetTargetMemberDefault()); }
private static bool TryAdjustToSingleUseableDataSource( ref IList <IDataSource> dataSources, IMemberMapperData mapperData) { var finalDataSource = dataSources.Last(); if (!finalDataSource.IsFallback) { return(false); } var finalValue = finalDataSource.Value; if (finalValue.NodeType == ExpressionType.Coalesce) { // Coalesce between the existing target member value and the fallback: dataSources[dataSources.Count - 1] = new AdHocDataSource( finalDataSource.SourceMember, ((BinaryExpression)finalValue).Right, finalDataSource.Condition, finalDataSource.Variables); return(false); } var targetMemberAccess = mapperData.GetTargetMemberAccess(); if (!ExpressionEvaluation.AreEqual(finalValue, targetMemberAccess)) { return(false); } if (dataSources.Count == 2) { return(true); } var dataSourcesWithoutFallback = new IDataSource[dataSources.Count - 1]; dataSourcesWithoutFallback.CopyFrom(dataSources); dataSources = dataSourcesWithoutFallback; return(false); }