Exemplo n.º 1
0
        public Expression BuildExpression(
            TypeMapRegistry typeMapRegistry,
            ParameterExpression srcParam, 
            ParameterExpression ctxtParam)
        {
            if (!CanResolve)
                return null;

            var ctorArgs = CtorParams.Select(p => p.CreateExpression(typeMapRegistry, srcParam, ctxtParam));

            ctorArgs =
                ctorArgs.Zip(Ctor.GetParameters(),
                    (exp, pi) => exp.Type == pi.ParameterType ? exp : Convert(exp, pi.ParameterType))
                    .ToArray();
            var newExpr = New(Ctor, ctorArgs);
            return newExpr;
        }
        public Expression CreateExpression(TypeMapRegistry typeMapRegistry,
            ParameterExpression srcParam,
            ParameterExpression ctxtParam,
            ref ParameterExpression parameterContext)
        {
            if (CustomExpression != null)
                return CustomExpression.ConvertReplaceParameters(srcParam).IfNotNull();

            if (CustomValueResolver != null)
            {
                return Invoke(Constant(CustomValueResolver), srcParam, ctxtParam);
            }

            if (!SourceMembers.Any() && Parameter.IsOptional)
            {
                return Constant(Parameter.GetDefaultValue());
            }

            if (typeMapRegistry.GetTypeMap(new TypePair(SourceType, DestinationType)) == null
                && Parameter.IsOptional)
            {
                return Constant(Parameter.GetDefaultValue());
            }

            var valueResolverExpr = SourceMembers.Aggregate(
                (Expression) srcParam,
                (inner, getter) => getter.MemberInfo is MethodInfo
                    ? getter.MemberInfo.IsStatic()
                        ? Call(null, (MethodInfo) getter.MemberInfo, inner)
                        : (Expression) Call(inner, (MethodInfo) getter.MemberInfo)
                    : MakeMemberAccess(getter.MemberInfo.IsStatic() ? null : inner, getter.MemberInfo)
                );
            valueResolverExpr = valueResolverExpr.IfNotNull();

            if ((SourceType.IsEnumerableType() && SourceType != typeof (string))
                || typeMapRegistry.GetTypeMap(new TypePair(SourceType, DestinationType)) != null
                || !DestinationType.IsAssignableFrom(SourceType))
            {
                /*
                var value = context.Mapper.Map(result, null, sourceType, destinationType, context);
                 */
                return TypeMapPlanBuilder.ContextMap(ToObject(valueResolverExpr), Constant(null), DestinationType, ref parameterContext);
            }
            return valueResolverExpr;
        }
Exemplo n.º 3
0
        public Expression BuildExpression(
            TypeMapRegistry typeMapRegistry,
            ParameterExpression srcParam, 
            ParameterExpression ctxtParam)
        {
            if (!CanResolve)
                return null;

            ParameterExpression parameterContext = null;
            var ctorArgs = CtorParams.Select(p => p.CreateExpression(typeMapRegistry, srcParam, ctxtParam, ref parameterContext));

            ctorArgs =
                ctorArgs.Zip(Ctor.GetParameters(),
                    (exp, pi) => exp.Type == pi.ParameterType ? exp : Convert(exp, pi.ParameterType))
                    .ToArray();

            var newExpr = New(Ctor, ctorArgs);
            if(parameterContext == null)
            {
                return newExpr;
            }
            var mapExpression = Invoke(Lambda(Block(new[] { parameterContext }, TypeMapPlanBuilder.CreatePropertyContext(parameterContext, ctxtParam), newExpr)));
            return mapExpression;
        }
Exemplo n.º 4
0
 public void Configure(TypeMapRegistry typeMapRegistry)
 {
     foreach (var typeMapConfiguration in _typeMapConfigs.Where(c => !c.IsOpenGeneric))
     {
         Configure(typeMapRegistry, typeMapConfiguration);
         if (typeMapConfiguration.ReverseTypeMap != null)
         {
             Configure(typeMapRegistry, typeMapConfiguration.ReverseTypeMap);
         }
     }
 }
Exemplo n.º 5
0
 private void ApplyDerivedMaps(TypeMapRegistry typeMapRegistry, TypeMap baseMap, TypeMap typeMap)
 {
     foreach (var inheritedTypeMap in typeMap.IncludedDerivedTypes.Select(typeMapRegistry.GetTypeMap).Where(map => map != null))
     {
         inheritedTypeMap.ApplyInheritedMap(baseMap);
         ApplyDerivedMaps(typeMapRegistry, baseMap, inheritedTypeMap);
     }
 }
Exemplo n.º 6
0
 private static void ApplyBaseMaps(TypeMapRegistry typeMapRegistry, TypeMap derivedMap, TypeMap currentMap)
 {
     foreach (var baseMap in currentMap.IncludedBaseTypes.Select(typeMapRegistry.GetTypeMap).Where(baseMap => baseMap != null))
     {
         baseMap.IncludeDerivedTypes(currentMap.SourceType, currentMap.DestinationType);
         derivedMap.ApplyInheritedMap(baseMap);
         ApplyBaseMaps(typeMapRegistry, derivedMap, baseMap);
     }
 }
Exemplo n.º 7
0
        public TypeMap ConfigureClosedGenericTypeMap(TypeMapRegistry typeMapRegistry, TypePair closedTypes, TypePair requestedTypes)
        {
            var openMapConfig = _openTypeMapConfigs
                //.Where(tm => tm.IsOpenGeneric)
                .Where(tm =>
                    tm.Types.SourceType.GetGenericTypeDefinitionIfGeneric() == closedTypes.SourceType.GetGenericTypeDefinitionIfGeneric() &&
                    tm.Types.DestinationType.GetGenericTypeDefinitionIfGeneric() == closedTypes.DestinationType.GetGenericTypeDefinitionIfGeneric())
                .OrderByDescending(tm => tm.DestinationType == closedTypes.DestinationType) // Favor more specific destination matches,
                .ThenByDescending(tm => tm.SourceType == closedTypes.SourceType) // then more specific source matches
                .FirstOrDefault();

            if (openMapConfig == null)
                return null;

            var closedMap = _typeMapFactory.CreateTypeMap(requestedTypes.SourceType, requestedTypes.DestinationType, this, openMapConfig.MemberList);

            openMapConfig.Configure(closedMap);

            Configure(typeMapRegistry, closedMap);

            if (closedMap.TypeConverterType != null)
            {
                var typeParams =
                    (openMapConfig.SourceType.IsGenericTypeDefinition() ? closedTypes.SourceType.GetGenericArguments() : new Type[0])
                        .Concat
                    (openMapConfig.DestinationType.IsGenericTypeDefinition() ? closedTypes.DestinationType.GetGenericArguments() : new Type[0]);

                var neededParameters = closedMap.TypeConverterType.GetGenericParameters().Length;
                closedMap.TypeConverterType = closedMap.TypeConverterType.MakeGenericType(typeParams.Take(neededParameters).ToArray());
            }
            if (closedMap.DestinationTypeOverride?.IsGenericTypeDefinition() == true)
            {
                var neededParameters = closedMap.DestinationTypeOverride.GetGenericParameters().Length;
                closedMap.DestinationTypeOverride = closedMap.DestinationTypeOverride.MakeGenericType(closedTypes.DestinationType.GetGenericArguments().Take(neededParameters).ToArray());
            }
            return closedMap;
        }
Exemplo n.º 8
0
        public TypeMap ConfigureConventionTypeMap(TypeMapRegistry typeMapRegistry, TypePair types)
        {
            if (!TypeConfigurations.Any(c => c.IsMatch(types)))
                return null;

            var typeMap = _typeMapFactory.CreateTypeMap(types.SourceType, types.DestinationType, this, MemberList.Destination);

            var config = new MappingExpression(typeMap.Types, typeMap.ConfiguredMemberList);

            config.Configure(typeMap);

            Configure(typeMapRegistry, typeMap);

            return typeMap;
        }
Exemplo n.º 9
0
        private void Configure(TypeMapRegistry typeMapRegistry, TypeMap typeMap)
        {
            foreach (var action in AllTypeMapActions)
            {
                var expression = new MappingExpression(typeMap.Types, typeMap.ConfiguredMemberList);

                action(typeMap, expression);

                expression.Configure(typeMap);
            }

            foreach (var action in AllPropertyMapActions)
            {
                foreach (var propertyMap in typeMap.GetPropertyMaps())
                {
                    var memberExpression = new MappingExpression.MemberConfigurationExpression(propertyMap.DestinationProperty, typeMap.SourceType);

                    action(propertyMap, memberExpression);

                    memberExpression.Configure(typeMap);
                }
            }

            ApplyBaseMaps(typeMapRegistry, typeMap, typeMap);
            ApplyDerivedMaps(typeMapRegistry, typeMap, typeMap);
        }
Exemplo n.º 10
0
 private void Configure(TypeMapRegistry typeMapRegistry, ITypeMapConfiguration typeMapConfiguration)
 {
     var typeMap = typeMapRegistry.GetTypeMap(typeMapConfiguration.Types);
     Configure(typeMapRegistry, typeMap);
 }
Exemplo n.º 11
0
        private void BuildTypeMap(TypeMapRegistry typeMapRegistry, ITypeMapConfiguration config)
        {
            var typeMap = _typeMapFactory.CreateTypeMap(config.SourceType, config.DestinationType, this, config.MemberList);

            config.Configure(typeMap);

            typeMapRegistry.RegisterTypeMap(typeMap);
        }
Exemplo n.º 12
0
        public void Register(TypeMapRegistry typeMapRegistry)
        {
            foreach (var config in _typeMapConfigs.Where(c => !c.IsOpenGeneric))
            {
                BuildTypeMap(typeMapRegistry, config);

                if (config.ReverseTypeMap != null)
                {
                    BuildTypeMap(typeMapRegistry, config.ReverseTypeMap);
                }
            }
        }
Exemplo n.º 13
0
        public void Seal(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider)
        {
            if (_sealed)
                return;

            foreach (var inheritedTypeMap in _inheritedTypeMaps)
            {
                ApplyInheritedTypeMap(inheritedTypeMap);
            }

            _orderedPropertyMaps =
                _propertyMaps
                    .Union(_inheritedMaps)
                    .OrderBy(map => map.MappingOrder).ToArray();

            MapExpression = TypeMapPlanBuilder.BuildMapperFunc(this, configurationProvider, typeMapRegistry);

            _sealed = true;
        }
Exemplo n.º 14
0
        public void Seal(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider)
        {
            if(_sealed)
            {
                return;
            }
            _sealed = true;

            foreach (var inheritedTypeMap in _inheritedTypeMaps)
            {
                ApplyInheritedTypeMap(inheritedTypeMap);
            }

            _orderedPropertyMaps =
                _propertyMaps
                    .Union(_inheritedMaps)
                    .OrderBy(map => map.MappingOrder).ToArray();

            MapExpression = new TypeMapPlanBuilder(configurationProvider, typeMapRegistry, this).CreateMapperLambda();
        }
Exemplo n.º 15
0
        public TypeMap CreateClosedGenericTypeMap(ITypeMapConfiguration openMapConfig, TypeMapRegistry typeMapRegistry, TypePair closedTypes, TypePair requestedTypes)
        {
            var closedMap = _typeMapFactory.CreateTypeMap(requestedTypes.SourceType, requestedTypes.DestinationType, this, openMapConfig.MemberList);

            openMapConfig.Configure(closedMap);

            Configure(typeMapRegistry, closedMap);

            if (closedMap.TypeConverterType != null)
            {
                var typeParams =
                    (openMapConfig.SourceType.IsGenericTypeDefinition() ? closedTypes.SourceType.GetGenericArguments() : new Type[0])
                    .Concat
                        (openMapConfig.DestinationType.IsGenericTypeDefinition() ? closedTypes.DestinationType.GetGenericArguments() : new Type[0]);

                var neededParameters = closedMap.TypeConverterType.GetGenericParameters().Length;
                closedMap.TypeConverterType = closedMap.TypeConverterType.MakeGenericType(typeParams.Take(neededParameters).ToArray());
            }
            if (closedMap.DestinationTypeOverride?.IsGenericTypeDefinition() == true)
            {
                var neededParameters = closedMap.DestinationTypeOverride.GetGenericParameters().Length;
                closedMap.DestinationTypeOverride = closedMap.DestinationTypeOverride.MakeGenericType(closedTypes.DestinationType.GetGenericArguments().Take(neededParameters).ToArray());
            }
            return(closedMap);
        }
Exemplo n.º 16
0
        private void Configure(TypeMapRegistry typeMapRegistry, ITypeMapConfiguration typeMapConfiguration)
        {
            var typeMap = typeMapRegistry.GetTypeMap(typeMapConfiguration.Types);

            Configure(typeMapRegistry, typeMap);
        }
Exemplo n.º 17
0
 public Expression MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
 {
     return(Call(Constant(this), MapMethod, sourceExpression, destExpression, contextExpression));
 }