public void Seal() { var derivedMaps = new List <Tuple <TypePair, TypeMap> >(); var redirectedTypes = new List <Tuple <TypePair, TypePair> >(); foreach (var typeMap in _userDefinedTypeMaps.Values) { typeMap.Seal(); _typeMapPlanCache.AddOrUpdate(typeMap.Types, typeMap, (_, _2) => typeMap); if (typeMap.DestinationTypeOverride != null) { redirectedTypes.Add(Tuple.Create(typeMap.Types, new TypePair(typeMap.SourceType, typeMap.DestinationTypeOverride))); } derivedMaps.AddRange(GetDerivedTypeMaps(typeMap).Select(derivedMap => Tuple.Create(new TypePair(derivedMap.SourceType, typeMap.DestinationType), derivedMap))); } foreach (var redirectedType in redirectedTypes) { var derivedMap = FindTypeMapFor(redirectedType.Item2); if (derivedMap != null) { _typeMapPlanCache.AddOrUpdate(redirectedType.Item1, derivedMap, (_, _2) => derivedMap); } } foreach (var derivedMap in derivedMaps) { _typeMapPlanCache.GetOrAdd(derivedMap.Item1, _ => derivedMap.Item2); } }
internal ProfileConfiguration GetProfile(string profileName) { ProfileConfiguration expr = _formatterProfiles.GetOrAdd(profileName, name => new ProfileConfiguration()); return(expr); }
public TypeMap CreateTypeMap(Type source, Type destination, string profileName, MemberList memberList) { var typePair = new TypePair(source, destination); var typeMap = _userDefinedTypeMaps.GetOrAdd(typePair, tp => { var profileConfiguration = GetProfile(profileName); var tm = _typeMapFactory.CreateTypeMap(source, destination, profileConfiguration, memberList); tm.Profile = profileName; tm.IgnorePropertiesStartingWith = _globalIgnore; IncludeBaseMappings(source, destination, tm); // keep the cache in sync TypeMap _; _typeMapPlanCache.TryRemove(tp, out _); OnTypeMapCreated(tm); return(tm); }); return(typeMap); }
public TypeMap ResolveTypeMap(TypePair typePair) { var typeMap = _typeMapPlanCache.GetOrAdd(typePair, _ => GetRelatedTypePairs(_) .Select(tp => FindTypeMapFor(tp) ?? _typeMapPlanCache.GetOrDefault(tp)) .FirstOrDefault(tm => tm != null)); return(typeMap); }
internal IProfileExpression GetProfile(string profileName) { var brandNew = _formatterProfiles.Keys.Count == 0; var expr = _formatterProfiles.GetOrAdd(profileName, name => new Profile(profileName)); if (brandNew) { var temp = expr.DefaultMemberConfig; } return(expr); }
public IMappingExpression CreateMap(Type sourceType, Type destinationType, MemberList memberList, string profileName) { if (sourceType.IsGenericTypeDefinition() && destinationType.IsGenericTypeDefinition()) { var typePair = new TypePair(sourceType, destinationType); var expression = _typeMapExpressionCache.GetOrAdd(typePair, tp => new CreateTypeMapExpression(tp, memberList, profileName)); return(expression); } var typeMap = CreateTypeMap(sourceType, destinationType, profileName, memberList); return(CreateMappingExpression(typeMap, destinationType)); }
public Expression CreateMapExpression(Type sourceType, Type destinationType, System.Collections.Generic.IDictionary <string, object> parameters = null, params MemberInfo[] membersToExpand) { parameters = parameters ?? new Dictionary <string, object>(); var cachedExpression = _expressionCache.GetOrAdd(new ExpressionRequest(sourceType, destinationType, membersToExpand), tp => CreateMapExpression(tp, DictionaryFactory.CreateDictionary <ExpressionRequest, int>())); if (!parameters.Any()) { return(cachedExpression); } var visitor = new ConstantExpressionReplacementVisitor(parameters); return(visitor.Visit(cachedExpression)); }
object IMappingEngineRunner.Map(ResolutionContext context) { try { var contextTypePair = new TypePair(context.SourceType, context.DestinationType); Func <TypePair, IObjectMapper> missFunc = tp => ConfigurationProvider.GetMappers().FirstOrDefault(mapper => mapper.IsMatch(context)); IObjectMapper mapperToUse = _objectMapperCache.GetOrAdd(contextTypePair, missFunc); if (mapperToUse == null || (context.Options.CreateMissingTypeMaps && !mapperToUse.IsMatch(context))) { if (context.Options.CreateMissingTypeMaps) { var typeMap = ConfigurationProvider.CreateTypeMap(context.SourceType, context.DestinationType); context = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType, context.DestinationType); mapperToUse = missFunc(contextTypePair); if (mapperToUse == null) { throw new AutoMapperMappingException(context, "Unsupported mapping."); } _objectMapperCache.AddOrUpdate(contextTypePair, mapperToUse, (tp, mapper) => mapperToUse); } else { if (context.SourceValue != null) { throw new AutoMapperMappingException(context, "Missing type map configuration or unsupported mapping."); } return(ObjectCreator.CreateDefaultValue(context.DestinationType)); } } return(mapperToUse.Map(context, this)); } catch (AutoMapperMappingException) { throw; } catch (Exception ex) { throw new AutoMapperMappingException(context, ex); } }