Beispiel #1
0
        private static IEnumerable <DictionaryTargetMember> GetParentContextFlattenedTargetMembers(
            ObjectMapperData mapperData,
            DictionaryTargetMember targetDictionaryMember)
        {
            while (mapperData.Parent != null)
            {
                mapperData = mapperData.Parent;

                var sourceMembers = GlobalContext.Instance
                                    .MemberCache
                                    .GetSourceMembers(mapperData.SourceType)
                                    .SelectMany(sm => MatchingFlattenedMembers(sm, targetDictionaryMember))
                                    .ToArray();

                var targetMembers = EnumerateTargetMembers(
                    sourceMembers,
                    targetDictionaryMember,
                    mapperData,
                    m => m.Name.StartsWithIgnoreCase(targetDictionaryMember.Name)
                       ? m.Name.Substring(targetDictionaryMember.Name.Length)
                       : m.Name);

                foreach (var targetMember in targetMembers)
                {
                    yield return(targetMember);
                }
            }
        }
Beispiel #2
0
        private static IEnumerable <DictionaryTargetMember> EnumerateTargetMembers(
            IEnumerable <Member> sourceMembers,
            DictionaryTargetMember targetDictionaryMember,
            ObjectMapperData mapperData,
            Func <Member, string> targetMemberNameFactory)
        {
            foreach (var sourceMember in sourceMembers)
            {
                var targetEntryMemberName = targetMemberNameFactory.Invoke(sourceMember);
                var targetEntryMember     = targetDictionaryMember.Append(sourceMember.DeclaringType, targetEntryMemberName);

                if (targetDictionaryMember.HasObjectEntries)
                {
                    targetEntryMember = (DictionaryTargetMember)targetEntryMember.WithType(sourceMember.Type);
                }

                var entryMapperData = new ChildMemberMapperData(targetEntryMember, mapperData);
                var configuredKey   = GetCustomKeyOrNull(entryMapperData);

                if (configuredKey != null)
                {
                    targetEntryMember.SetCustomKey(configuredKey);
                }

                if (!sourceMember.IsSimple && !targetDictionaryMember.HasComplexEntries)
                {
                    targetEntryMember = targetEntryMember.WithTypeOf(sourceMember);
                }

                yield return(targetEntryMember);
            }
        }
 public DictionaryPopulationBuilder(EnumerablePopulationBuilder wrappedBuilder)
 {
     _wrappedBuilder         = wrappedBuilder;
     _sourceDictionaryMember = wrappedBuilder.MapperData.GetDictionarySourceMemberOrNull();
     _targetDictionaryMember = (DictionaryTargetMember)MapperData.TargetMember;
     _mappingExpressions     = new List <Expression>();
 }
        private bool IsDictionaryEntry(LambdaExpression targetMemberLambda, out DictionaryTargetMember entryMember)
        {
            if (targetMemberLambda.Body.NodeType != ExpressionType.Call)
            {
                entryMember = null;
                return(false);
            }

            var methodCall = (MethodCallExpression)targetMemberLambda.Body;

            if (!methodCall.Method.IsSpecialName ||
                (methodCall.Method.Name != "get_Item") ||
                !methodCall.Method.DeclaringType.IsDictionary())
            {
                // TODO: Test coverage - specified, non-dictionary indexed target member
                entryMember = null;
                return(false);
            }

            var entryKeyExpression = methodCall.Arguments[0];

            if (entryKeyExpression.NodeType != ExpressionType.Constant)
            {
                throw new MappingConfigurationException(
                          "Target dictionary entry keys must be constant string values.");
            }

            var entryKey = (string)((ConstantExpression)entryKeyExpression).Value;

            var rootMember = (DictionaryTargetMember)CreateRootTargetQualifiedMember();

            entryMember = rootMember.Append(typeof(TSource), entryKey);
            return(true);
        }
        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);
        }
 public ConfiguredDictionaryEntryDataSourceFactory(
     MappingConfigInfo configInfo,
     ConfiguredLambdaInfo dataSourceLambda,
     DictionaryTargetMember targetDictionaryEntryMember)
     : base(configInfo, dataSourceLambda, QualifiedMember.All)
 {
     TargetDictionaryEntryMember = targetDictionaryEntryMember;
 }
Beispiel #7
0
 public static Member DictionaryEntry(string sourceMemberName, DictionaryTargetMember targetMember)
 {
     return(new Member(
                MemberType.DictionaryEntry,
                sourceMemberName,
                targetMember.Type,
                targetMember.ValueType));
 }
        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);
        }
        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));
        }