public MappingStrategy(MappingInfo mappingInfo, IMappingDescriptor descriptor) { Descriptor = descriptor; Source = mappingInfo.MappingSourceType; Target = mappingInfo.MappingTargetType; HasTargetInstance = mappingInfo.MapIntoExistingTargetInstance; try { TargetConstructor = Target.GetConstructors().Single(); ConstructorParameterMappingSteps = new OrderedKeyedCollection <ParameterInfo, MappingStep>(TargetConstructor.GetParameters()); } catch (InvalidOperationException) { throw new ArgumentException("Target type must have single public constructor. This is the only scenario supported at the moment.", "target"); } ContextExpression = Expression.Parameter(typeof(MappingContext), "context"); SourceExpression = Expression.Variable(Source, "source"); TargetExpression = Expression.Variable(Target, "target"); MapperExpression = Expression.Property(ContextExpression, MappingContextMeta.Mapper); }
public MappingStrategy BuildMappingStrategy(MappingInfo mappingInfo) { var strategy = new MappingStrategy(mappingInfo, descriptor); //first try to shortcircuit var directMappingStep = new DirectMappingStep(strategy.Source, strategy.Target); var converter = ApplyConverter(directMappingStep, withFallback: false); if (converter != null) { directMappingStep.Conversion = converter; strategy.InitTargetStep = directMappingStep; return(strategy); } foreach (var pattern in mappingPatterns) { pattern.Contribute(strategy); } foreach (var mappingStep in strategy.MappingSteps) { mappingStep.Conversion = ApplyConverter(mappingStep, withFallback: true); } if (strategy.HasTargetInstance) { strategy.InitTargetStep = new SimpleStep(strategy.Target, strategy.Target, (s, _) => Expression.Convert(Expression.Property(s.ContextExpression, MappingContextMeta.TargetInstance), s.Target)); } else { foreach (var mappingStep in strategy.ConstructorParameterMappingSteps.ByKey) { if (mappingStep.Value == null) { throw new InvalidOperationException(string.Format("No mapping for constructor parameter {0} has been specified. All constructor parameters need value", mappingStep.Key)); } mappingStep.Value.Conversion = ApplyConverter(mappingStep.Value, withFallback: true); } strategy.InitTargetStep = new SimpleStep(strategy.Target, strategy.Target, (s, _) => Expression.New(s.TargetConstructor, GetConstructorParameters(s))); } return(strategy); }
public IIMappingBag Add(MappingInfo mappingInfo) { mappings.Add(mappingInfo); return(this); }