private void ThrowIfRedundantSourceMember(ConfiguredLambdaInfo valueLambdaInfo, QualifiedMember targetMember)
        {
            if (!valueLambdaInfo.TryGetSourceMember(out var sourceMemberLambda))
            {
                return;
            }

            var mappingData = _configInfo.ToMappingData <TSource, TTarget>();

            var targetMemberMapperData  = new ChildMemberMapperData(targetMember, mappingData.MapperData);
            var targetMemberMappingData = mappingData.GetChildMappingData(targetMemberMapperData);
            var bestSourceMemberMatch   = SourceMemberMatcher.GetMatchFor(targetMemberMappingData);

            if (!bestSourceMemberMatch.IsUseable)
            {
                return;
            }

            var configuredSourceMember = sourceMemberLambda.ToSourceMember(MapperContext);

            if (!bestSourceMemberMatch.SourceMember.Matches(configuredSourceMember))
            {
                return;
            }

            throw new MappingConfigurationException(string.Format(
                                                        CultureInfo.InvariantCulture,
                                                        "Source member {0} will automatically be mapped to target {1} {2}, " +
                                                        "and does not need to be configured",
                                                        GetSourceMemberDescription(configuredSourceMember),
                                                        GetTargetMemberType(targetMember),
                                                        targetMember.GetFriendlyTargetPath(_configInfo)));
        }
示例#2
0
        private static IEnumerable <IDataSource> GetSourceMemberDataSources(
            IEnumerable <IConfiguredDataSource> configuredDataSources,
            int dataSourceIndex,
            IChildMemberMappingData mappingData)
        {
            var bestMatchingSourceMember       = SourceMemberMatcher.GetMatchFor(mappingData);
            var matchingSourceMemberDataSource = GetSourceMemberDataSourceOrNull(bestMatchingSourceMember, mappingData);

            if ((matchingSourceMemberDataSource == null) ||
                configuredDataSources.Any(cds => cds.IsSameAs(matchingSourceMemberDataSource)))
            {
                if (dataSourceIndex == 0)
                {
                    if (mappingData.MapperData.TargetMember.IsComplex)
                    {
                        yield return(new ComplexTypeMappingDataSource(dataSourceIndex, mappingData));
                    }
                }
                else
                {
                    yield return(FallbackDataSourceFor(mappingData));
                }

                yield break;
            }

            yield return(matchingSourceMemberDataSource);

            if (matchingSourceMemberDataSource.IsConditional)
            {
                yield return(FallbackDataSourceFor(mappingData));
            }
        }
示例#3
0
        private static IDataSource GetSourceMemberDataSourceOrNull(DataSourceFindContext context)
        {
            var bestMatchingSourceMember = SourceMemberMatcher.GetMatchFor(
                context.ChildMappingData,
                out var contextMappingData);

            if (bestMatchingSourceMember == null)
            {
                return(null);
            }

            var sourceMemberDataSource = SourceMemberDataSource
                                         .For(bestMatchingSourceMember, contextMappingData.MapperData);

            return(context.GetFinalDataSource(sourceMemberDataSource, contextMappingData));
        }
示例#4
0
        private IQualifiedMember GetMatchingSourceMember <TSource, TTarget>(
            TSource source,
            TTarget target,
            Expression <Func <TTarget, object> > childMemberExpression)
        {
            var targetMember = TargetMemberFor(childMemberExpression);

            var mappingContext  = new SimpleMappingContext(DefaultMapperContext.RuleSets.CreateNew, DefaultMapperContext);
            var rootMappingData = ObjectMappingDataFactory.ForRoot(source, target, mappingContext);
            var rootMapperData  = rootMappingData.MapperData;

            var childMapperData     = new ChildMemberMapperData(targetMember, rootMapperData);
            var childMappingContext = rootMappingData.GetChildMappingData(childMapperData);

            return(SourceMemberMatcher.GetMatchFor(childMappingContext, out _));
        }
示例#5
0
        public void ShouldNotMatchSameNameIncompatibleTypeProperties()
        {
            var source = new TwoValues {
                Value = new int[5], value = string.Empty
            };
            var target       = new PublicProperty <byte>();
            var targetMember = TargetMemberFor <PublicProperty <byte> >(x => x.Value);

            var mappingContext  = new MappingExecutor <TwoValues>(DefaultMapperContext.RuleSets.CreateNew, DefaultMapperContext);
            var rootMappingData = mappingContext.CreateRootMappingData(source, target);
            var rootMapperData  = rootMappingData.MapperData;

            var childMapperData     = new ChildMemberMapperData(targetMember, rootMapperData);
            var childMappingContext = rootMappingData.GetChildMappingData(childMapperData);

            var matchingSourceMember = SourceMemberMatcher.GetMatchFor(childMappingContext);

            matchingSourceMember.ShouldNotBeNull();
            matchingSourceMember.Name.ShouldBe("value");
        }
示例#6
0
        private void ThrowIfRedundantSourceMember(ConfiguredLambdaInfo valueLambdaInfo, QualifiedMember targetMember)
        {
            if (!valueLambdaInfo.IsSourceMember(out var sourceMemberLambda))
            {
                return;
            }

            var mappingData = _configInfo.ToMappingData <TSource, TTarget>();

            var targetMemberMapperData   = new ChildMemberMapperData(targetMember, mappingData.MapperData);
            var targetMemberMappingData  = mappingData.GetChildMappingData(targetMemberMapperData);
            var bestMatchingSourceMember = SourceMemberMatcher.GetMatchFor(targetMemberMappingData, out _);

            if (bestMatchingSourceMember == null)
            {
                return;
            }

            var sourceMember = sourceMemberLambda.ToSourceMember(MapperContext);

            if (!bestMatchingSourceMember.Matches(sourceMember))
            {
                return;
            }

            var targetMemberType = (targetMember.LeafMember.MemberType == MemberType.ConstructorParameter)
                ? "constructor parameter"
                : "member";

            throw new MappingConfigurationException(string.Format(
                                                        CultureInfo.InvariantCulture,
                                                        "Source member {0} will automatically be mapped to target {1} {2}, " +
                                                        "and does not need to be configured",
                                                        GetSourceMemberDescription(sourceMember),
                                                        targetMemberType,
                                                        targetMember.GetFriendlyTargetPath(_configInfo)));
        }
示例#7
0
        private static bool TryGetMetaMember(
            IList <string> memberNameParts,
            DataSourceFindContext context,
            out MetaMemberPartBase metaMember)
        {
            var currentMappingData  = default(IObjectMappingData);
            var currentSourceMember = default(IQualifiedMember);
            var currentTargetMember = default(QualifiedMember);
            var currentMemberPart   = metaMember = default(MetaMemberPartBase);

            Func <IQualifiedMember, QualifiedMember, IObjectMappingData, DataSourceFindContext, IObjectMappingData> currentMappingDataFactory =
                (sm, tm, md, c) => c.ChildMappingData.Parent;

            for (var i = memberNameParts.Count - 1; i >= 0; --i)
            {
                var memberNamePart = memberNameParts[i];

                switch (memberNamePart)
                {
                case HasMetaMemberPart.Name:
                    if (HasMetaMemberPart.TryCreateFor(context.MapperData, ref currentMemberPart))
                    {
                        break;
                    }

                    return(false);

                case FirstMetaMemberPart.Name:
                    currentMemberPart = new FirstMetaMemberPart(context.MapperData);
                    break;

                case LastMetaMemberPart.Name:
                    currentMemberPart = new LastMetaMemberPart(context.MapperData);
                    break;

                case CountMetaMemberPart.Name:
                    if (CountMetaMemberPart.TryCreateFor(context.MapperData, ref currentMemberPart))
                    {
                        break;
                    }

                    return(false);

                case NumberOfMetaMemberPart.Name:
                    if (NumberOfMetaMemberPart.TryCreateFor(context.MapperData, ref currentMemberPart))
                    {
                        break;
                    }

                    return(false);

                default:
                    currentMappingData = currentMappingDataFactory.Invoke(
                        currentSourceMember,
                        currentTargetMember,
                        currentMappingData,
                        context);

                    var currentMapperData = currentMappingData.MapperData;

                    var matchingTargetMember = GlobalContext.Instance
                                               .MemberCache
                                               .GetTargetMembers(currentMapperData.TargetType)
                                               .FirstOrDefault(m => m.Name == memberNamePart);

                    if (matchingTargetMember == null)
                    {
                        matchingTargetMember = GlobalContext.Instance
                                               .MemberCache
                                               .GetSourceMembers(currentMapperData.SourceType)
                                               .FirstOrDefault(m => m.Name == memberNamePart);

                        if (matchingTargetMember == null)
                        {
                            return(false);
                        }
                    }

                    currentTargetMember = currentMapperData.TargetMember.Append(matchingTargetMember);

                    var childMemberMapperData = new ChildMemberMapperData(currentTargetMember, currentMapperData);

                    var memberMappingData = currentMappingData.GetChildMappingData(childMemberMapperData);

                    currentSourceMember = SourceMemberMatcher.GetMatchFor(
                        memberMappingData,
                        out _,
                        searchParentContexts: false);

                    if (currentSourceMember == null)
                    {
                        return(false);
                    }

                    currentMemberPart = new SourceMemberMetaMemberPart(
                        currentSourceMember,
                        currentMapperData,
                        isRootMemberPart: currentMemberPart == null);

                    currentMappingDataFactory = (sm, tm, md, c) =>
                    {
                        var mappingData = ObjectMappingDataFactory.ForChild(sm, tm, 0, md);

                        return(sm.IsEnumerable
                                ? ObjectMappingDataFactory.ForElement(mappingData)
                                : ObjectMappingDataFactory.ForChild(sm, tm, 0, md));
                    };

                    break;
                }

                if (metaMember == null)
                {
                    metaMember = currentMemberPart;
                    continue;
                }

                if (!metaMember.TrySetNextPart(currentMemberPart))
                {
                    return(false);
                }
            }

            return(true);
        }