public override Expression BuildConversionExpression(MappingStrategy strategy) { var parameters = expression.Parameters; var visitor = new DelegatingConversionVisitor(strategy, parameters[0], parameters[1], parameters[2]); return(visitor.Visit(expression.Body)); }
public void Contribute(MappingStrategy strategy) { var properties = strategy.Source.GetProperties(); foreach (var targetProperty in strategy.Target.GetProperties()) { var sourceProperty = Array.Find(properties, p => p.Name == targetProperty.Name); if (sourceProperty != null) { var assign = new Assign(targetProperty, sourceProperty); strategy.AddMappingStep(assign); } } foreach (var mappingStep in strategy.ConstructorParameterMappingSteps.ByKey) { if (mappingStep.Value == null) { var sourceProperty = Array.Find(properties, p => string.Equals(p.Name, mappingStep.Key.Name, StringComparison.OrdinalIgnoreCase)); if (sourceProperty != null) { var assign = new ConstructorAssign(mappingStep.Key, sourceProperty); mappingStep.UpdateValue(assign); } } } }
/// <summary> /// Specify an explicit property mapping /// </summary> /// <param name="nodeType">Type of the source node - one of Element or Attribute</param> /// <param name="sourceNodeName">Name of the source node to get</param> /// <param name="targetPropertyExpression">Accessor for the target property to set</param> /// <returns></returns> public XDocumentMap <TTarget> Specify(XmlNodeType nodeType, XName sourceNodeName, Expression <Func <TTarget, object> > targetPropertyExpression) { var mapping = GetNodeMapping(nodeType, sourceNodeName, targetPropertyExpression); MappingStrategy.AddMapping(mapping); return(this); }
public object Map(object source, object destination) { if (destination == null && this.Options.Safety.ThrowIfDestinationIsNull) { throw new ArgumentNullException("destination"); } var pair = new TypePair(source.GetType(), destination.GetType()); MemberMap map; if (!this.maps.TryGetValue(pair, out map)) { map = MappingStrategy.CreateMapProposal(pair).FinalizeMap(); } if (BeforeMapping != null) { BeforeMapping(this, pair, source); } var result = map.MappingFunction.DynamicInvoke(source, destination); if (AfterMapping != null) { AfterMapping(this, pair, result); } return(result); }
/// <summary> /// Specify an explicit property mapping /// </summary> /// <param name="mappingAction">Action to populate target property value</param> /// <returns></returns> protected internal Map <TSource, TTarget, TMatchingLookup> Specify(Action <TSource, TTarget> mappingAction) { var mapping = new ActionMapping <TSource, TTarget>(mappingAction); MappingStrategy.AddMapping(mapping); return(this); }
/// <summary> /// Specify an explicit property mapping /// </summary> /// <typeparam name="TInput">Source property type</typeparam> /// <typeparam name="TOutput">Target property type</typeparam> /// <param name="sourcePropertyExpression">Accessor for the source property to get</param> /// <param name="targetPropertyExpression">Accessor for the target property to set</param> /// <returns></returns> public ClassMap <TSource, TTarget> Specify <TInput, TOutput>(Func <TSource, TInput> sourcePropertyExpression, Expression <Func <TTarget, TOutput> > targetPropertyExpression) { var mapping = new FuncMapping <TSource, TInput, TTarget, TOutput>(sourcePropertyExpression, targetPropertyExpression); MappingStrategy.AddMapping(mapping); return(this); }
public void Contribute(MappingStrategy strategy) { var sourceProperties = strategy.Source.GetProperties(); foreach (var targetProperty in strategy.Target.GetProperties()) { var sourcePropertyChain = BuildPropertyChain(targetProperty.Name, sourceProperties); if (sourcePropertyChain.Length > 1) { strategy.AddMappingStep(new AssignChain(targetProperty, sourcePropertyChain)); } } foreach (var mappingStep in strategy.ConstructorParameterMappingSteps.ByKey) { if (mappingStep.Value == null) { var targetParameter = mappingStep.Key; var sourcePropertyChain = BuildPropertyChain(targetParameter.Name, sourceProperties); if (sourcePropertyChain.Length > 1) { mappingStep.UpdateValue(new ConstructorAssignChain(mappingStep.Key, sourcePropertyChain)); } } } }
public DelegatingConversionVisitor(MappingStrategy strategy, ParameterExpression sourceValueParameter, ParameterExpression mapperParameter, ParameterExpression contextParameter) { this.strategy = strategy; this.sourceValueParameter = sourceValueParameter; this.mapperParameter = mapperParameter; this.contextParameter = contextParameter; }
/// <summary> /// Specify an explicit property mapping /// </summary> /// <param name="sourceColumnName">Name of the source column to get</param> /// <param name="targetPropertyExpression">Accessor for the target property to set</param> /// <returns></returns> public DataReaderMap <TTarget> Specify(string sourceColumnName, Expression <Func <TTarget, object> > targetPropertyExpression) { var mapping = new DataReaderColumnMapping(sourceColumnName, targetPropertyExpression); MappingStrategy.AddMapping(mapping); return(this); }
/// <summary> /// Specify an explicit property mapping /// </summary> /// <param name="sourceFieldIndex">Index of the source field to get</param> /// <param name="targetPropertyExpression">Accessor for the target property to set</param> /// <returns></returns> public CsvMap <TTarget> Specify(int souceFieldIndex, Expression <Func <TTarget, object> > targetPropertyExpression) { var mapping = new CsvFieldMapping(souceFieldIndex, targetPropertyExpression); MappingStrategy.AddMapping(mapping); return(this); }
/// <summary> /// Specify an explicit property mapping /// </summary> /// <param name="xPathQuery">XPath query for the source node to get, starting from root</param> /// <param name="targetPropertyExpression">Accessor for the target property to set</param> /// <returns></returns> public XDocumentMap <TTarget> Specify(string xPathQuery, Expression <Func <TTarget, object> > targetPropertyExpression) { var mapping = new XPathMapping(xPathQuery, targetPropertyExpression); MappingStrategy.AddMapping(mapping); return(this); }
private IFilter CreateRoutingFilter(MappingStrategy strategy = MappingStrategy.None) { var configuration = new RoutingConfiguration { Mappings = new Dictionary <string, Mapping>() }; configuration.Mappings.Add("test", new Mapping { Path = Path, PathReplace = ReplacedPath, Strategy = strategy, TargetHosts = new[] { TargetHost.Value } }); var configurationMock = new Mock <IConfigurationContainer>(); configurationMock.Setup(x => x.Configure(It.IsAny <string>(), It.IsAny <Action <RoutingConfiguration> >())) .Callback <string, Action <RoutingConfiguration> >((config, action) => { action.Invoke(configuration); }); var httpClientFactoryMock = new Mock <IHttpClientFactory>(); httpClientFactoryMock.Setup(x => x.Create(It.IsAny <HttpClientConfiguration>())) .Returns(new HttpClient()); var filterActionFactoryMock = new Mock <IFilterActionFactory>(); filterActionFactoryMock.Setup(x => x.CreateFilterAction(It.IsAny <FilterAction>(), It.IsAny <IEnumerable <string> >())) .Returns <FilterAction, IEnumerable <string> >((fa, array) => fa); return(new RouterFilter(configurationMock.Object, httpClientFactoryMock.Object, filterActionFactoryMock.Object)); }
/// <summary> /// Specify an explicit property mapping with a value conversion /// </summary> /// <typeparam name="TInput">Source property type</typeparam> /// <typeparam name="TOutput">Target property type</typeparam> /// <param name="xPathQuery">XPath query for the source node to get, starting from root</param> /// <param name="targetPropertyExpression">Accessor for the target property to set</param> /// <param name="conversion">Conversion function to apply to the source value</param> /// <returns></returns> public XDocumentMap <TTarget> Specify <TInput, TOutput>(string xPathQuery, Expression <Func <TTarget, TOutput> > targetPropertyExpression, Func <TInput, TOutput> conversion) { var mapping = new XPathMapping(xPathQuery, targetPropertyExpression); mapping.SetConversion <TInput, TOutput>(conversion); MappingStrategy.AddMapping(mapping); return(this); }
/// <summary> /// Specify an explicit property mapping with a value conversion /// </summary> /// <typeparam name="TInput">Source property type</typeparam> /// <typeparam name="TOutput">Target property type</typeparam> /// <param name="sourceColumnName">Name of the source column to get</param> /// <param name="targetPropertyExpression">Accessor for the target property to set</param> /// <param name="conversion">Conversion function to apply to the source value</param> /// <returns></returns> public DataReaderMap <TTarget> Specify <TInput, TOutput>(string sourceColumnName, Expression <Func <TTarget, TOutput> > targetPropertyExpression, Func <TInput, TOutput> conversion) { var mapping = new DataReaderColumnMapping(sourceColumnName, targetPropertyExpression); mapping.SetConversion <TInput, TOutput>(conversion); MappingStrategy.AddMapping(mapping); return(this); }
/// <summary> /// Specify an explicit property mapping with a value conversion /// </summary> /// <exception cref="ArgumentException"> /// Thrown if XmlNodeType is not Element or Attribute /// </exception> /// <typeparam name="TInput">Source property type</typeparam> /// <typeparam name="TOutput">Target property type</typeparam> /// <param name="nodeType">Type of the source node - one of Element or Attribute</param> /// <param name="sourceNodeName">Name of the source node to get</param> /// <param name="targetPropertyExpression">Accessor for the target property to set</param> /// <param name="conversion">Conversion function to apply to the source value</param> /// <returns></returns> public XDocumentMap <TTarget> Specify <TInput, TOutput>(XmlNodeType nodeType, XName sourceNodeName, Expression <Func <TTarget, TOutput> > targetPropertyExpression, Func <TInput, TOutput> conversion) { var mapping = GetNodeMapping(nodeType, sourceNodeName, targetPropertyExpression); mapping.SetConversion <TInput, TOutput>(conversion); MappingStrategy.AddMapping(mapping); return(this); }
/// <summary> /// Specify an explicit property mapping with a value conversion /// </summary> /// <typeparam name="TInput">Source property type</typeparam> /// <typeparam name="TOutput">Target property type</typeparam> /// <param name="sourceFieldIndex">Index of the source field to get</param> /// <param name="targetPropertyExpression">Accessor for the target property to set</param> /// <param name="conversion">Conversion function to apply to the source value</param> /// <returns></returns> public CsvMap <TTarget> Specify <TInput, TOutput>(int souceFieldIndex, Expression <Func <TTarget, TOutput> > targetPropertyExpression, Func <TInput, TOutput> conversion) { var mapping = new CsvFieldMapping(souceFieldIndex, targetPropertyExpression); mapping.SetConversion <TInput, TOutput>(conversion); MappingStrategy.AddMapping(mapping); return(this); }
public override Expression Apply(MappingStrategy strategy, ConversionStep conversion) { if (nullableProperties == null) { var value = sourcePropertyChain.Aggregate <PropertyInfo, Expression>(strategy.SourceExpression, Expression.Property); return(SetValue(strategy, conversion, value)); } return(BuildBody(Expression.Property(strategy.SourceExpression, sourcePropertyChain[0]), 0, strategy, conversion)); }
Expression SetValue(MappingStrategy strategy, ConversionStep conversion, Expression value) { strategy.ValueExpression = value; if (conversion != null) { var convert = conversion.BuildConversionExpression(strategy); strategy.ValueExpression = convert; } return(BuildSetTargetValueExpression(strategy)); }
/// <summary> /// Creates an expression that returns TDestination as output from a given TSource (a 'projection'). /// </summary> /// <typeparam name="TSource"></typeparam> /// <typeparam name="TDestination"></typeparam> /// <returns></returns> public Expression <Func <TSource, TDestination> > Project <TSource, TDestination>() { var pair = new TypePair(typeof(TSource), typeof(TDestination)); Projection projection; if (!this.projections.TryGetValue(pair, out projection)) { projection = MappingStrategy.CreateMapProposal(pair).FinalizeProjection(); } return((Expression <Func <TSource, TDestination> >)projection.Expression); }
public override Expression Apply(MappingStrategy strategy, ConversionStep conversion) { var get = Expression.Property(strategy.SourceExpression, sourceProperty); strategy.ValueExpression = get; if (conversion != null) { var convert = conversion.BuildConversionExpression(strategy); strategy.ValueExpression = convert; } return(strategy.ValueExpression); }
public void Contribute(MappingStrategy strategy) { foreach (var targetProperty in strategy.Target.GetProperties().Where(p => p.Name.EndsWith(targetPropertyNameSuffix, StringComparison.OrdinalIgnoreCase))) { var expectedName = GetExpectedName(targetProperty); var sourceProperty = strategy.Source.GetProperties().FirstOrDefault(p => p.Name == expectedName); if (sourceProperty != null) { var assign = new Assign(targetProperty, sourceProperty); strategy.AddMappingStep(assign); } } }
Expression BuildBody(Expression owner, int index, MappingStrategy strategy, ConversionStep conversion) { if (index == sourcePropertyChain.Length - 1) { return(SetValue(strategy, conversion, owner)); } if (nullableProperties.Contains(sourcePropertyChain[index]) == false) { return(BuildBody(Expression.Property(owner, sourcePropertyChain[index + 1]), index + 1, strategy, conversion)); } var local = Expression.Variable(owner.Type); var property = new PropertyIfNotNullInnerExpression(Expression.Property(local, sourcePropertyChain[index + 1])); var body = BuildBody(property, index + 1, strategy, conversion); var expression = new PropertyIfNotNullExpression(owner, body, local, TargetValueType); property.Owner = expression; return(expression); }
public TDestination Map <TDestination>(object source) where TDestination : new() { if (source == null) { var option = this.Options.Safety.IfSourceIsNull; if (option == SourceObjectNullOptions.ReturnDestinationObject) { return(new TDestination()); } else if (option == SourceObjectNullOptions.ReturnNullWhenSourceIsNull) { return(default(TDestination)); } } var pair = new TypePair(source.GetType(), typeof(TDestination)); MemberMap map; if (!this.maps.TryGetValue(pair, out map)) { map = MappingStrategy.CreateMapProposal(pair).FinalizeMap(); } var destination = new TDestination(); if (BeforeMapping != null) { BeforeMapping(this, pair, source); } var result = (TDestination)map.MappingFunction.DynamicInvoke(source, destination); if (AfterMapping != null) { AfterMapping(this, pair, result); } return(result); }
/// <summary> /// Before populating a target object, generates mappings or loads from cache /// </summary> /// <param name="target"></param> /// <param name="source"></param> protected virtual void OnBeforePopulate(TTarget target, TSource source) { if (CachingStrategy is NullCachingStrategy && !MappingStrategy.GeneratedMappings) { MappingStrategy.GetMappings(source, typeof(TTarget), MatchingStrategy, AutoMapUnspecifiedTargets); } else { var cacheKey = GetCacheKey(); var cachedMappings = CachingStrategy.Get(cacheKey) as List <IPropertyMapping>; if (cachedMappings == null) { cachedMappings = MappingStrategy.GetMappings(source, typeof(TTarget), MatchingStrategy, AutoMapUnspecifiedTargets); CachingStrategy.Set(cacheKey, cachedMappings); } else { MappingStrategy.SetMappings(cachedMappings); } } }
public TDestination Map <TSource, TDestination>(TSource source, TDestination destination) { if (destination == null && this.Options.Safety.ThrowIfDestinationIsNull) { throw new ArgumentNullException("destination"); } var pair = new TypePair(typeof(TSource), typeof(TDestination)); MemberMap map; if (!this.maps.TryGetValue(pair, out map)) { map = MappingStrategy.CreateMapProposal(pair).FinalizeMap(); } if (BeforeMapping != null) { BeforeMapping(this, pair, source); } var func = map.MappingFunction as Func <TSource, TDestination, TDestination>; if (func == null) { throw new InvalidOperationException(string.Format("The mapping from {0} to {1} is not configured to be called without parameters. Use another overload of Map or recreate the map without a parameter.", pair.SourceType, pair.DestinationType)); } var result = func(source, destination); if (AfterMapping != null) { AfterMapping(this, pair, result); } return(result); }
public override Expression Apply(MappingStrategy strategy, ConversionStep conversion) { strategy.ValueExpression = strategy.SourceExpression; return(conversion.BuildConversionExpression(strategy)); }
public IModelAnnotationsMapper Map(MappedType type, MappingStrategy strategy = MappingStrategy.TablePerType) { entityList.Add(type, strategy); return(this); }
Expression BuildSetTargetValueExpression(MappingStrategy context) { var property = Expression.Property(context.TargetExpression, targetProperty); return(Expression.Assign(property, context.ValueExpression)); }
public abstract Expression BuildConversionExpression(MappingStrategy strategy);
/// <summary> /// Populate an existing target object from source /// </summary> /// <param name="source"></param> /// <param name="target"></param> public void Populate(TSource source, TTarget target) { OnBeforePopulate(target, source); MappingStrategy.Populate(target, source); }