public static IMappingExpression <TSource, TDestination> ForCtorParamMatching <TSource, TDestination, TMember>( this IMappingExpression <TSource, TDestination> mappingExpression, Expression <Func <TDestination, TMember> > destinationMember, Action <ICtorParamConfigurationExpression <TSource> > paramOptions) { var memberInfo = ReflectionHelper.FindProperty(destinationMember); var camelCaseParam = char.ToLowerInvariant(memberInfo.Name[0]) + memberInfo.Name.Substring(1); return(mappingExpression.ForCtorParam(camelCaseParam, paramOptions)); }
public static IMappingExpression <T1, T2> ConstructorFromProperties <T1, T2>(this IMappingExpression <T1, T2> mapper) { var constructor = typeof(T2).GetConstructors().First(); foreach (var param in constructor.GetParameters()) { mapper = mapper.ForCtorParam(param.Name, opt => opt.MapFrom(param.Name !.ToPascalCase())); } return(mapper); }
/// <summary> /// Instructs the AutoMapper profile to resolve the specified <paramref name="constructorParam"/> by checking the resolution context for an /// already created item matching the <paramref name="keyAttributes"/> of the source's reference property. /// </summary> /// <param name="expression"> /// The expression. /// </param> /// <param name="resolutionSource"> /// The resolution source. /// </param> /// <param name="resolutionTarget"> /// The resolution target. /// </param> /// <param name="constructorParam"> /// The constructor param. /// </param> /// <param name="keyAttributes"> /// The key attributes. /// </param> /// <typeparam name="TSource"> /// The type of item that is the source of the mapping expression. /// </typeparam> /// <typeparam name="TReference"> /// The type of property on the source that will be used as the reference to resolve on the target. /// </typeparam> /// <typeparam name="TDest"> /// The type of item that is the destination of the mapping expression. /// </typeparam> /// <typeparam name="TTarget"> /// The type of property on the destination that will be resolved. /// </typeparam> /// <returns> /// The current <see cref="IMappingExpression{TSource,TDest}"/>. /// </returns> public static IMappingExpression <TSource, TDest> ResolveByKey <TSource, TReference, TDest, TTarget>( [NotNull] this IMappingExpression <TSource, TDest> expression, [NotNull] Expression <Func <TSource, TReference> > resolutionSource, [NotNull] Expression <Func <TDest, TTarget> > resolutionTarget, [NotNull] string constructorParam, [NotNull] params Expression <Func <TReference, object> >[] keyAttributes) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } if (resolutionSource == null) { throw new ArgumentNullException(nameof(resolutionSource)); } if (resolutionTarget == null) { throw new ArgumentNullException(nameof(resolutionTarget)); } if (constructorParam == null) { throw new ArgumentNullException(nameof(constructorParam)); } if (keyAttributes == null) { throw new ArgumentNullException(nameof(keyAttributes)); } expression.ForCtorParam( constructorParam, configurationExpression => configurationExpression.MapFrom( (source, context) => ResolveItem <TReference, TTarget>(context, resolutionSource.Compile().Invoke(source), keyAttributes.ToArray()))); expression.ForMember( resolutionTarget, configurationExpression => configurationExpression.MapFrom( (source, dest, target, context) => ResolveItem <TReference, TTarget>( context, resolutionSource.Compile().Invoke(source), keyAttributes.ToArray()))); return(expression); }