Exemple #1
0
        public bool WillNotMap(IQualifiedMemberContext context)
        {
            if (!context.TargetMember.IsRecursion)
            {
                return(false);
            }

            if (context.SourceType.IsDictionary())
            {
                return(context.TargetMember.Depth > 3);
            }

            while (context != null)
            {
                if (context.TargetMember.IsDictionary ||
                    context.TargetType.Name.EndsWith("Dto", StringComparison.Ordinal) ||
                    context.TargetType.Name.EndsWith("DataTransferObject", StringComparison.Ordinal))
                {
                    return(true);
                }

                context = context.Parent;
            }

            return(false);
        }
        public override bool AppliesTo(IQualifiedMemberContext context)
        {
            if (!base.AppliesTo(context))
            {
                return(false);
            }

            var applicableDictionaryContext = ConfigInfo.Get <DictionaryContext>();

            if (applicableDictionaryContext == All)
            {
                return(true);
            }

            while (context != null)
            {
                if (context.TargetMember.IsDictionary)
                {
                    return(false);
                }

                context = context.Parent;
            }

            return(true);
        }
Exemple #3
0
        private IEnumerable <string> EnumerateMatchingNames(Member member, IQualifiedMemberContext context)
        {
            var matchingName = default(string);

            for (var i = 0; i < _matchingNameFactories.Count; ++i)
            {
                var factory = _matchingNameFactories[i];

                if (!factory.AppliesTo(context))
                {
                    continue;
                }

                matchingName = factory.GetMemberName(member);

                if (matchingName != null)
                {
                    yield return(matchingName);

                    break;
                }
            }

            if (member.Name != matchingName)
            {
                yield return(member.Name);
            }
        }
        public static bool IsRepeatMapping(this IQualifiedMemberContext context)
        {
            if (context.IsRoot || (context.TargetMember.Depth == 2))
            {
                return(false);
            }

            if (context.TargetMember.IsRecursion)
            {
                return(true);
            }

            if ((context.TargetMember.Depth == 3) && context.TargetMemberIsEnumerableElement())
            {
                return(false);
            }

            if (TargetMemberHasRecursiveObjectGraph(context.TargetMember) == false)
            {
                return(false);
            }

            // The target member we're mapping right now isn't recursive, but it has recursion
            // within its child members, and its mapping might be repeated elsewhere within the
            // mapping graph. We therefore check if this member ever repeats; if so we'll map it
            // by calling MapRepeated, and it'll be the entry point of the RepeatedMapperFunc
            // which performs the repeated mapping:
            var rootMember = context.GetRootMapperData().TargetMember;

            return(TargetMemberEverRepeatsWithin(rootMember, context.TargetMember));
        }
        private bool HasTypeBeenMapped(Type targetType, IQualifiedMemberContext requestingMapperData)
        {
            var mappedType = TargetMember.IsEnumerable ? TargetMember.ElementType : TargetType;

            if (targetType.IsAssignableTo(mappedType))
            {
                return(true);
            }

            foreach (var childMapperData in ChildMapperDatasOrEmpty)
            {
                if (childMapperData == requestingMapperData)
                {
                    break;
                }

                if (childMapperData.HasTypeBeenMapped(targetType, this))
                {
                    return(true);
                }
            }

            if ((Parent != null) && (requestingMapperData != Parent))
            {
                return(Parent.HasTypeBeenMapped(targetType, this));
            }

            return(false);
        }
Exemple #6
0
 public Expression GetCallbackOrNull(
     InvocationPosition position,
     IQualifiedMemberContext context,
     IMemberMapperData mapperData)
 {
     return(_mappingCallbackFactories?.FirstOrDefault(f => f.AppliesTo(position, context))?.Create(mapperData));
 }
 public static IList <DerivedTypePair> GetDerivedTypePairs(this IQualifiedMemberContext context)
 {
     return(context
            .MapperContext
            .UserConfigurations
            .DerivedTypes
            .GetDerivedTypePairsFor(context));
 }
        public static ObjectMapperData GetRootMapperData(this IQualifiedMemberContext context)
        {
            while (!context.IsRoot)
            {
                context = context.Parent;
            }

            return((ObjectMapperData)context);
        }
Exemple #9
0
        public bool ShortCircuitRecursion(IQualifiedMemberContext context)
        {
            if (_recursionDepthSettings == null)
            {
                return(true);
            }

            return(RecursionDepthSettings.FindMatch(context)?.IsBeyondDepth(context) != false);
        }
Exemple #10
0
        public bool MapEntityKeys(IQualifiedMemberContext context)
        {
            var applicableSetting = _entityKeyMappingSettings?
                                    .FirstOrDefault(context, (ctx, s) => s.AppliesTo(ctx))?
                                    .MapKeys;

            return((applicableSetting == true) ||
                   (context.RuleSet.Settings.AllowEntityKeyMapping && (applicableSetting != false)));
        }
        public IList <DerivedTypePair> GetImplementationTypePairsFor(IQualifiedMemberContext context)
        {
            if (_typePairsByTargetType.TryGetValue(context.TargetType, out var typePairs))
            {
                return(typePairs
                       .FilterToArray(context, (md, tp) => tp.IsImplementationPairing && tp.AppliesTo(md)));
            }

            return(Enumerable <DerivedTypePair> .EmptyArray);
        }
        public override bool AppliesTo(IQualifiedMemberContext context)
        {
            if (context.TargetMember.IsDictionary)
            {
                return(false);
            }

            return(context.TargetMember.Matches(TargetDictionaryEntryMember) &&
                   base.AppliesTo(context));
        }
 public static Expression GetMappingCallbackOrNull(
     this IQualifiedMemberContext context,
     InvocationPosition invocationPosition,
     IMemberMapperData mapperData)
 {
     return(mapperData
            .MapperContext
            .UserConfigurations
            .GetCallbackOrNull(invocationPosition, context, mapperData));
 }
Exemple #14
0
        private static IQualifiedMember CreateSourceMember(Expression value, IQualifiedMemberContext context)
        {
            var sourceMember = new ConfiguredSourceMember(value, context);

            var finalSourceMember = context.MapperContext
                                    .QualifiedMemberFactory
                                    .GetFinalSourceMember(sourceMember, context.TargetMember);

            return(finalSourceMember);
        }
Exemple #15
0
 private EnumMappingMismatch(
     IQualifiedMember targetMember,
     IEnumerable <IQualifiedMember> sourceMembers,
     IList <string> mismatches,
     IQualifiedMemberContext context)
 {
     _rootMapperData  = context.GetRootMapperData();
     _sourceMembers   = sourceMembers;
     _mismatches      = mismatches;
     TargetMemberPath = targetMember.GetFriendlyTargetPath(_rootMapperData);
 }
Exemple #16
0
 private MapperDataContext(
     ObjectMapperData mapperData,
     bool isStandalone,
     bool isForDerivedType,
     IQualifiedMemberContext context)
 {
     _mapperData      = mapperData;
     IsStandalone     = isStandalone;
     IsForDerivedType = isForDerivedType;
     UseLocalVariable = isForDerivedType || ShouldUseLocalVariable(context);
 }
Exemple #17
0
        private static bool UseParameterlessConstructor(
            DictionarySourceMember sourceDictionaryMember,
            IQualifiedMemberContext context)
        {
            if (sourceDictionaryMember.Type.IsInterface())
            {
                return(true);
            }

            return(sourceDictionaryMember.ValueType != context.TargetMember.ElementType);
        }
Exemple #18
0
        private static bool UseDictionaryCloneConstructor(
            IQualifiedMember sourceDictionaryMember,
            IQualifiedMemberContext context,
            out ConstructorInfo cloneConstructor)
        {
            cloneConstructor = null;

            return((sourceDictionaryMember.Type == context.TargetType) &&
                   context.TargetMember.ElementType.IsSimple() &&
                   ((cloneConstructor = GetDictionaryCloneConstructor(context)) != null));
        }
        private static bool ShortCircuitRecursion(IQualifiedMemberContext context)
        {
            if (!context.TargetMember.IsRecursion)
            {
                return(false);
            }

            return(context
                   .MapperContext
                   .UserConfigurations
                   .ShortCircuitRecursion(context));
        }
Exemple #20
0
 public ConfiguredSourceMember(Expression value, IQualifiedMemberContext context)
     : this(
         value,
         value.Type,
         value.Type.IsEnumerable(),
         value.Type.IsSimple(),
         value.ToReadableString(),
         context.TargetMember.JoinedNames,
         context.MapperContext,
         GetConfiguredMemberChainOrNull(value, context))
 {
     _isMatchedToRootTarget = context.TargetMember.IsRoot;
 }
Exemple #21
0
        public EnumerablePopulationContext(IQualifiedMemberContext context)
        {
            SourceElementType = context.SourceMember.ElementType;

            if (SourceElementType == null)
            {
                return;
            }

            TargetElementType       = context.TargetMember.GetElementType(SourceElementType);
            ElementTypes            = new[] { SourceElementType, TargetElementType };
            ElementTypesAreTheSame  = SourceElementType == TargetElementType;
            TargetElementsAreSimple = TargetElementType.IsSimple();
        }
 public QualifiedMemberContext(
     IQualifiedMember sourceMember,
     QualifiedMember targetMember,
     IQualifiedMemberContext parent)
     : this(
         parent.RuleSet,
         parent.SourceType,
         parent.TargetType,
         sourceMember,
         targetMember,
         parent,
         parent.MapperContext)
 {
 }
        private static bool DoNotApplyFilter(
            IQualifiedMember sourceMember,
            IDataSource dataSource,
            IQualifiedMemberContext context)
        {
            if (!dataSource.IsValid)
            {
                return(true);
            }

            // Non-simple enumerable elements will be filtered out elsewhere,
            // unless they're being runtime-typed:
            return(!sourceMember.IsSimple && !context.IsEntryPoint &&
                   context.TargetMemberIsEnumerableElement());
        }
Exemple #24
0
        private static bool IsPartOfExpandoObjectMapping(IQualifiedMemberContext context)
        {
            while (context != null)
            {
                if ((context.SourceMember.GetFriendlyTypeName() == nameof(ExpandoObject)) ||
                    (context.TargetMember.GetFriendlyTypeName() == nameof(ExpandoObject)))
                {
                    return(true);
                }

                context = context.Parent;
            }

            return(false);
        }
Exemple #25
0
        public override bool AppliesTo(IQualifiedMemberContext context)
        {
            if (!base.AppliesTo(context))
            {
                return(false);
            }

            if (context.SourceType.IsAssignableTo(DerivedSourceType))
            {
                return(true);
            }

            return(_isInterfacePairing &&
                   context.SourceType.IsAssignableTo(SourceType) &&
                   context.TargetType.IsAssignableTo(TargetType));
        }
        public IList <DerivedTypePair> GetDerivedTypePairsFor(IQualifiedMemberContext context)
        {
            LookForDerivedTypePairs(context);

            if (_typePairsByTargetType.None())
            {
                return(Enumerable <DerivedTypePair> .EmptyArray);
            }

            if (_typePairsByTargetType.TryGetValue(context.TargetType, out var typePairs))
            {
                return(typePairs.FilterToArray(context, (ctx, tp) => tp.AppliesTo(ctx)));
            }

            return(Enumerable <DerivedTypePair> .EmptyArray);
        }
        public QualifiedMember SetContext(IQualifiedMemberContext context)
        {
            Context = context;

            if (IsRoot || (JoinedNames?.Any() != false))
            {
                return(this);
            }

            var matchingNameSets = MemberChain.ProjectToArray(
                context,
                (ctx, m) => ctx.MapperContext.Naming.GetMatchingNamesFor(m, ctx));

            JoinedNames = _mapperContext.Naming.GetJoinedNamesFor(matchingNameSets);
            return(this);
        }
        private static Expression GetBranchedPopulation(
            Expression condition,
            Expression population,
            Expression alternatePopulation,
            IDataSource alternateDataSource,
            IQualifiedMemberContext memberContext)
        {
            if (alternateDataSource.IsSequential && !memberContext.TargetMember.IsSimple)
            {
                return(Expression.Block(
                           Expression.IfThen(condition, population),
                           alternatePopulation));
            }

            return(Expression.IfThenElse(condition, population, alternatePopulation));
        }
Exemple #29
0
        private QualifiedMember GetTargetMember(Member member, IQualifiedMemberContext context)
        {
            if (context.TargetMember.LeafMember == member)
            {
                return(context.TargetMember);
            }

            var memberIndex       = Array.LastIndexOf(context.TargetMember.MemberChain, member);
            var targetMemberChain = new Member[memberIndex + 1];

            for (var i = 0; i < targetMemberChain.Length; i++)
            {
                targetMemberChain[i] = context.TargetMember.MemberChain[i];
            }

            return(QualifiedMember.Create(targetMemberChain, ConfigInfo.MapperContext));
        }
Exemple #30
0
        public bool AppliesTo(Member member, IQualifiedMemberContext context)
        {
            if (!base.AppliesTo(context))
            {
                return(false);
            }

            if (((ConfigInfo.SourceValueType ?? Constants.AllTypes) != Constants.AllTypes) &&
                (context.SourceType.GetDictionaryTypes().Value != ConfigInfo.SourceValueType))
            {
                return(false);
            }

            var applicableDictionaryType = ConfigInfo.Get <DictionaryType>();

            if (IsPartOfExpandoObjectMapping(context) !=
                (applicableDictionaryType == DictionaryType.Expando))
            {
                return(false);
            }

            if (SourceMember == null)
            {
                return(true);
            }

            var targetMember = GetTargetMember(member, context);

            if (targetMember.Depth < SourceMember.Depth)
            {
                return(false);
            }

            var targetMemberChainIndex = targetMember.Depth;

            for (var i = SourceMember.Depth - 1; i > 0; --i)
            {
                if (!SourceMember.MemberChain[i].Equals(targetMember.MemberChain[--targetMemberChainIndex]))
                {
                    return(false);
                }
            }

            return(true);
        }