public void Should_include_full_type_name_when_missing_map() { QueryMapperHelper .MissingMapException(typeof(QueryMapperHelperTests), typeof(QueryMapperHelperTests)) .Message.ShouldStartWith( "Missing map from " + typeof(QueryMapperHelperTests).FullName ); }
public void CheckProjection() { if (Projection) { throw new AutoMapperConfigurationException( "CreateProjection works with ProjectTo, not with Map.", QueryMapperHelper.MissingMapException(Types) ); } }
public Expression CreateMapExpression(ExpressionRequest request, Expression instanceParameter, ConcurrentDictionary <ExpressionRequest, int> typePairCount) { var typeMap = _configurationProvider.ResolveTypeMap(request.SourceType, request.DestinationType); if (typeMap == null) { throw QueryMapperHelper.MissingMapException(request.SourceType, request.DestinationType); } var parameterReplacer = instanceParameter is ParameterExpression ? new ParameterReplacementVisitor(instanceParameter) : null; var customProjection = typeMap.CustomProjection; if (customProjection != null) { return(parameterReplacer == null ? customProjection.Body : parameterReplacer.Visit(customProjection.Body)); } var bindings = new List <MemberBinding>(); var visitCount = typePairCount.AddOrUpdate(request, 0, (tp, i) => i + 1); if (visitCount >= typeMap.MaxDepth) { if (_configurationProvider.AllowNullDestinationValues) { return(null); } } else { bindings = CreateMemberBindings(request, typeMap, instanceParameter, typePairCount); } Expression constructorExpression = typeMap.DestinationConstructorExpression(instanceParameter); if (parameterReplacer != null) { constructorExpression = parameterReplacer.Visit(constructorExpression); } var visitor = new NewFinderVisitor(); visitor.Visit(constructorExpression); var expression = Expression.MemberInit( visitor.NewExpression, bindings.ToArray() ); return(expression); }
private Expression CreateMapExpressionCore(ExpressionRequest request, Expression instanceParameter, TypePairCount typePairCount, LetPropertyMaps letPropertyMaps, out TypeMap typeMap) { typeMap = _configurationProvider.ResolveTypeMap(request.SourceType, request.DestinationType); if (typeMap == null) { throw QueryMapperHelper.MissingMapException(request.SourceType, request.DestinationType); } if (typeMap.CustomProjection != null) { return(typeMap.CustomProjection.ReplaceParameters(instanceParameter)); } return(CreateMapExpressionCore(request, instanceParameter, typePairCount, typeMap, letPropertyMaps)); }
private IEnumerable <TypeMap> GetDerivedTypeMaps(TypeMap typeMap) { foreach (var derivedMap in typeMap.IncludedDerivedTypes.Select(FindTypeMapFor)) { if (derivedMap == null) { throw QueryMapperHelper.MissingMapException(typeMap.SourceType, typeMap.DestinationType); } yield return(derivedMap); foreach (var derivedTypeMap in GetDerivedTypeMaps(derivedMap)) { yield return(derivedTypeMap); } } }
public Expression CreateMapExpression(ExpressionRequest request, Expression instanceParameter, IDictionary <ExpressionRequest, int> typePairCount) { var typeMap = _configurationProvider.ResolveTypeMap(request.SourceType, request.DestinationType); if (typeMap == null) { throw QueryMapperHelper.MissingMapException(request.SourceType, request.DestinationType); } if (typeMap.CustomProjection != null) { return(typeMap.CustomProjection.ReplaceParameters(instanceParameter)); } var bindings = new List <MemberBinding>(); var depth = GetDepth(request, typePairCount); if (typeMap.MaxDepth > 0 && depth >= typeMap.MaxDepth) { if (typeMap.Profile.AllowNullDestinationValues) { return(null); } } else { bindings = CreateMemberBindings(request, typeMap, instanceParameter, typePairCount); } Expression constructorExpression = DestinationConstructorExpression(typeMap, instanceParameter); if (instanceParameter is ParameterExpression) { constructorExpression = ((LambdaExpression)constructorExpression).ReplaceParameters(instanceParameter); } var visitor = new NewFinderVisitor(); visitor.Visit(constructorExpression); var expression = MemberInit( visitor.NewExpression, bindings.ToArray() ); return(expression); }
private static IEnumerable <TypeMap> GetIncludedTypeMaps(IEnumerable <TypePair> includedTypes, IConfigurationProvider configurationProvider) { foreach (var pair in includedTypes) { var typeMap = configurationProvider.FindTypeMapFor(pair); if (typeMap != null) { yield return(typeMap); } typeMap = configurationProvider.ResolveTypeMap(pair); // we want the exact map the user included, but we could instantiate an open generic if (typeMap == null || typeMap.Types != pair || typeMap.IsConventionMap) { throw QueryMapperHelper.MissingMapException(pair); } yield return(typeMap); } }
public IEnumerable <TypeMap> GetIncludedTypeMaps(IEnumerable <TypePair> includedTypes) { foreach (var pair in includedTypes) { var typeMap = FindTypeMapFor(pair); if (typeMap != null) { yield return(typeMap); } else { typeMap = ResolveTypeMap(pair); // we want the exact map the user included, but we could instantiate an open generic if (typeMap == null || typeMap.Types != pair) { throw QueryMapperHelper.MissingMapException(pair); } yield return(typeMap); } } }
protected void FindDestinationFullName(Type typeSource, Type typeDestination, string sourceFullName, List <PropertyMapInfo> propertyMapInfoList) { const string period = "."; if (typeSource == typeDestination) { var sourceFullNameArray = sourceFullName.Split(new[] { period[0] }, StringSplitOptions.RemoveEmptyEntries); sourceFullNameArray.Aggregate(propertyMapInfoList, (list, next) => { if (list.Count == 0) { AddPropertyMapInfo(typeSource, next, list); } else { var last = list[list.Count - 1]; AddPropertyMapInfo(last.CustomExpression == null ? last.DestinationPropertyInfos[last.DestinationPropertyInfos.Count - 1].GetMemberType() : last.CustomExpression.ReturnType, next, list); } return(list); }); return; } var typeMap = ConfigurationProvider.ResolveTypeMap(sourceType: typeDestination, destinationType: typeSource);//The destination becomes the source because to map a source expression to a destination expression, //we need the expressions used to create the source from the destination if (typeMap == null) { throw QueryMapperHelper.MissingMapException(sourceType: typeDestination, destinationType: typeSource); } PathMap pathMap = typeMap.FindPathMapByDestinationPath(sourceFullName); if (pathMap != null) { propertyMapInfoList.Add(new PropertyMapInfo(pathMap.SourceExpression, new List <MemberInfo>())); return; } if (sourceFullName.IndexOf(period, StringComparison.OrdinalIgnoreCase) < 0) { var propertyMap = typeMap.GetPropertyMaps().SingleOrDefault(item => item.DestinationProperty.Name == sourceFullName); var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationProperty.Name); if (propertyMap.ValueResolverConfig != null) { throw new InvalidOperationException(Resource.customResolversNotSupported); } if (propertyMap.CustomExpression != null) { if (propertyMap.CustomExpression.ReturnType.IsValueType() && sourceMemberInfo.GetMemberType() != propertyMap.CustomExpression.ReturnType) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.expressionMapValueTypeMustMatchFormat, propertyMap.CustomExpression.ReturnType.Name, propertyMap.CustomExpression.ToString(), sourceMemberInfo.GetMemberType().Name, propertyMap.DestinationProperty.Name)); } } else { if (propertyMap.SourceMember.GetMemberType().IsValueType() && sourceMemberInfo.GetMemberType() != propertyMap.SourceMember.GetMemberType()) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.expressionMapValueTypeMustMatchFormat, propertyMap.SourceMember.GetMemberType().Name, propertyMap.SourceMember.Name, sourceMemberInfo.GetMemberType().Name, propertyMap.DestinationProperty.Name)); } } propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomExpression, propertyMap.SourceMembers.ToList())); } else { var propertyName = sourceFullName.Substring(0, sourceFullName.IndexOf(period, StringComparison.OrdinalIgnoreCase)); var propertyMap = typeMap.GetPropertyMaps().SingleOrDefault(item => item.DestinationProperty.Name == propertyName); var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationProperty.Name); if (propertyMap.CustomExpression == null && propertyMap.SourceMember == null)//If sourceFullName has a period then the SourceMember cannot be null. The SourceMember is required to find the ProertyMap of its child object. { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.srcMemberCannotBeNullFormat, typeSource.Name, typeDestination.Name, propertyName)); } propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomExpression, propertyMap.SourceMembers.ToList())); var childFullName = sourceFullName.Substring(sourceFullName.IndexOf(period, StringComparison.OrdinalIgnoreCase) + 1); FindDestinationFullName(sourceMemberInfo.GetMemberType(), propertyMap.CustomExpression == null ? propertyMap.SourceMember.GetMemberType() : propertyMap.CustomExpression.ReturnType, childFullName, propertyMapInfoList); } }
private Expression CreateMapExpressionCore(ExpressionRequest request, Expression instanceParameter, TypePairCount typePairCount, LetPropertyMaps letPropertyMaps, out TypeMap typeMap) { typeMap = _configurationProvider.ResolveTypeMap(request.SourceType, request.DestinationType) ?? throw QueryMapperHelper.MissingMapException(request.SourceType, request.DestinationType); return(CreateMapExpressionCore(request, instanceParameter, typePairCount, typeMap, letPropertyMaps)); }