public DictionaryPopulationBuilder(EnumerablePopulationBuilder wrappedBuilder)
 {
     _wrappedBuilder         = wrappedBuilder;
     _sourceDictionaryMember = wrappedBuilder.MapperData.GetDictionarySourceMemberOrNull();
     _targetDictionaryMember = (DictionaryTargetMember)MapperData.TargetMember;
     _mappingExpressions     = new List <Expression>();
 }
Пример #2
0
 private static bool SourceMemberIsDictionary(
     IMemberMapperData mapperData,
     out DictionarySourceMember sourceDictionaryMember)
 {
     sourceDictionaryMember = mapperData.GetDictionarySourceMemberOrNull();
     return(sourceDictionaryMember != null);
 }
Пример #3
0
        private static Expression GetValueParsing(
            DictionarySourceMember sourceMember,
            Expression variable,
            IChildMemberMappingData childMappingData)
        {
            var childMapperData = childMappingData.MapperData;
            var potentialNames  = GetPotentialNames(childMapperData);
            var fallbackValue   = GetFallbackValue(sourceMember, variable, potentialNames, childMappingData);

            if ((childMapperData.TargetMember.IsEnumerable != variable.Type.IsEnumerable()) &&
                (variable.Type != typeof(object)))
            {
                return(fallbackValue);
            }

            var tryGetValueCall = GetTryGetValueCall(
                variable,
                potentialNames.Select(Expression.Constant),
                childMapperData);

            var dictionaryValueOrFallback = Expression.Condition(
                tryGetValueCall,
                GetValue(sourceMember, variable, childMappingData),
                fallbackValue);

            return(dictionaryValueOrFallback);
        }
 public SourceElementsDictionaryAdapter(
     DictionarySourceMember sourceMember,
     EnumerablePopulationBuilder builder)
     : base(builder)
 {
     _dictionaryVariables = new DictionaryEntryVariablePair(sourceMember, builder.MapperData);
 }
 public DictionaryEntryVariablePair(DictionarySourceMember sourceMember, IMemberMapperData mapperData)
 {
     SourceMember         = sourceMember;
     MapperData           = mapperData;
     _targetMemberName    = GetTargetMemberName(mapperData);
     UseDirectValueAccess = sourceMember.ValueType.IsAssignableTo(mapperData.TargetMember.Type);
     Variables            = UseDirectValueAccess ? new[] { Key } : new[] { Key, Value };
 }
 public SourceObjectDictionaryAdapter(
     DictionarySourceMember sourceMember,
     EnumerablePopulationBuilder builder)
     : base(builder)
 {
     _instanceDictionaryAdapter = new SourceInstanceDictionaryAdapter(sourceMember, builder);
     _emptyTarget = TargetTypeHelper.GetEmptyInstanceCreation(TargetTypeHelper.EnumerableInterfaceType);
 }
 public SourceInstanceDictionaryAdapter(
     DictionarySourceMember sourceMember,
     EnumerablePopulationBuilder builder)
     : base(builder)
 {
     _defaultAdapter     = new DefaultSourceEnumerableAdapter(builder);
     DictionaryVariables = new DictionaryEntryVariablePair(sourceMember, builder.MapperData);
 }
 public DictionaryToDictionaryPopulationLoopData(
     DictionarySourceMember sourceMember,
     ObjectMapperData mapperData)
     : base(
         mapperData.EnumerablePopulationBuilder,
         typeof(KeyValuePair <,>).MakeGenericType(sourceMember.KeyType, sourceMember.ValueType),
         mapperData.SourceObject)
 {
 }
Пример #9
0
 private DictionaryDataSource(
     DictionarySourceMember sourceMember,
     ParameterExpression variable,
     IChildMemberMappingData childMappingData)
     : base(
         sourceMember,
         new[] { variable },
         GetValueParsing(sourceMember, variable, childMappingData))
 {
 }
Пример #10
0
        private static bool UseParameterlessConstructor(
            DictionarySourceMember sourceDictionaryMember,
            IBasicMapperData mapperData)
        {
            if (sourceDictionaryMember.Type.IsInterface())
            {
                return(true);
            }

            return(sourceDictionaryMember.ValueType != mapperData.TargetMember.ElementType);
        }
Пример #11
0
        private static bool UseParameterlessConstructor(
            DictionarySourceMember sourceDictionaryMember,
            IQualifiedMemberContext context)
        {
            if (sourceDictionaryMember.Type.IsInterface())
            {
                return(true);
            }

            return(sourceDictionaryMember.ValueType != context.TargetMember.ElementType);
        }
Пример #12
0
        private static Expression GetEnumerablePopulation(
            DictionarySourceMember sourceMember,
            Expression variable,
            IEnumerable <string> potentialNames,
            IChildMemberMappingData childMappingData)
        {
            var sourceList = Expression.Variable(typeof(List <>).MakeGenericType(sourceMember.EntryType), "sourceList");
            var counter    = Expression.Variable(typeof(int), "i");

            var potentialNameConstants = GetPotentialItemNames(potentialNames, counter, childMappingData.MapperData);

            var tryGetValueCall       = GetTryGetValueCall(variable, potentialNameConstants, childMappingData.MapperData);
            var loopBreak             = Expression.Break(Expression.Label());
            var ifNotTryGetValueBreak = Expression.IfThen(Expression.Not(tryGetValueCall), loopBreak);

            var sourceListAddCall = Expression.Call(sourceList, "Add", Constants.NoTypeArguments, variable);
            var incrementCounter  = Expression.PreIncrementAssign(counter);

            var loopBody = Expression.Block(
                ifNotTryGetValueBreak,
                sourceListAddCall,
                incrementCounter);

            var populationLoop = Expression.Loop(loopBody, loopBreak.Target);

            var entrySourceMember = sourceMember.WithType(sourceList.Type);

            var mapping = MappingFactory.GetChildMapping(
                entrySourceMember,
                sourceList,
                0,
                childMappingData);

            var enumerablePopulation = Expression.Block(
                new[] { sourceList, counter },
                Expression.Assign(sourceList, sourceList.Type.GetEmptyInstanceCreation()),
                Expression.Assign(counter, Expression.Constant(0)),
                populationLoop,
                mapping);

            return(enumerablePopulation);
        }
Пример #13
0
        private static bool DictionaryEntriesCouldBeEnumerableElements(
            DictionarySourceMember sourceMember,
            IChildMemberMappingData childMappingData)
        {
            if (!childMappingData.MapperData.TargetMember.IsEnumerable)
            {
                return(false);
            }

            if (sourceMember.EntryType == typeof(object))
            {
                return(true);
            }

            var targetElementsAreCompatibleWithEntries = childMappingData.MapperData
                                                         .TargetMember.ElementType
                                                         .IsAssignableFrom(sourceMember.EntryType);

            return(targetElementsAreCompatibleWithEntries);
        }
Пример #14
0
        private static Expression GetFallbackValue(
            DictionarySourceMember sourceMember,
            Expression variable,
            IEnumerable <string> potentialNames,
            IChildMemberMappingData childMappingData)
        {
            if (DictionaryEntriesCouldBeEnumerableElements(sourceMember, childMappingData))
            {
                return(GetEnumerablePopulation(
                           sourceMember,
                           variable,
                           potentialNames,
                           childMappingData));
            }

            return(childMappingData
                   .RuleSet
                   .FallbackDataSourceFactory
                   .Create(childMappingData)
                   .Value);
        }
Пример #15
0
        private static Expression GetMappedDictionaryAssignment(
            DictionarySourceMember sourceDictionaryMember,
            IObjectMappingData mappingData)
        {
            var mapperData = mappingData.MapperData;

            if (UseParameterlessConstructor(sourceDictionaryMember, mapperData))
            {
                return(GetParameterlessDictionaryAssignment(mappingData));
            }

            var comparerProperty = mapperData.SourceObject.Type.GetPublicInstanceProperty("Comparer");

            var comparer = Expression.Property(mapperData.SourceObject, comparerProperty);

            var constructor = FindDictionaryConstructor(
                mapperData.TargetType,
                comparer.Type,
                numberOfParameters: 1);

            var dictionaryConstruction = Expression.New(constructor, comparer);

            return(GetDictionaryAssignment(dictionaryConstruction, mappingData));
        }