public MapperConfiguration Register <TSource, TDestination, TPropertyType>( Expression <Func <TSource, TPropertyType> > sourcePropertyAccessor, Expression <Func <TDestination, TPropertyType> > destinationPropertyAccessor) where TDestination : new() { if (sourcePropertyAccessor == null) { throw new ArgumentNullException(nameof(sourcePropertyAccessor)); } if (destinationPropertyAccessor == null) { throw new ArgumentNullException(nameof(destinationPropertyAccessor)); } PropertyInfo sourceProperty = GetPropertyInfo(sourcePropertyAccessor); PropertyInfo destinationProperty = GetPropertyInfo(destinationPropertyAccessor); if (!destinationProperty.CanWrite) { throw new ArgumentException("Destination property doesn't have setter."); } Type sourceType = sourceProperty.PropertyType; Type destinationType = destinationProperty.PropertyType; if (!TypesHelper.CanAssign(sourceType, destinationType)) { throw new ArgumentException("Incompatible types."); } var mappingTypesPair = new MappingTypesPair { Source = typeof(TSource), Destination = typeof(TDestination) }; var newMapping = new MappingPropertiesPair { SourceProperty = sourceProperty, DestinationProperty = destinationProperty }; List <MappingPropertiesPair> registeredMappings; if (!_configuration.TryGetValue(mappingTypesPair, out registeredMappings)) { registeredMappings = new List <MappingPropertiesPair>(); _configuration[mappingTypesPair] = registeredMappings; } registeredMappings.Add(newMapping); return(this); }
// Static internals private static List <MappingPropertiesPair> GetMappingProperties(MappingTypesPair mappingEntryInfo) { IEnumerable <MappingPropertiesPair> result = (from sourceProperty in mappingEntryInfo.Source.GetProperties() join destinationProperty in mappingEntryInfo.Destination.GetProperties() on sourceProperty.Name equals destinationProperty.Name where destinationProperty.CanWrite && TypesHelper.CanAssign(sourceProperty.PropertyType, destinationProperty.PropertyType) select new MappingPropertiesPair { SourceProperty = sourceProperty, DestinationProperty = destinationProperty }); return(result.ToList()); }
// Internals internal IEnumerable <MappingPropertiesPair> GetRegisteredMappings <TSource, TDestination>() { var mappingEntryInfo = new MappingTypesPair() { Source = typeof(TSource), Destination = typeof(TDestination) }; List <MappingPropertiesPair> result; if (!_configuration.TryGetValue(mappingEntryInfo, out result)) { result = new List <MappingPropertiesPair>(); } return(result); }
public TDestination Map <TSource, TDestination>(TSource source) where TDestination : new() { if (source == null) { throw new ArgumentNullException(nameof(source)); } var mappingEntryInfo = new MappingTypesPair() { Source = typeof(TSource), Destination = typeof(TDestination) }; Func <TSource, TDestination> mappingFunc = GetMappingFunction <TSource, TDestination>(mappingEntryInfo); return(mappingFunc(source)); }
//Internals private Func <TSource, TDestination> GetMappingFunction <TSource, TDestination>(MappingTypesPair mappingEntryInfo) where TDestination : new() { Func <TSource, TDestination> result; if (_mappingFunctionsCache.HasCacheFor(mappingEntryInfo)) { result = _mappingFunctionsCache.GetCacheFor <TSource, TDestination>(mappingEntryInfo); } else { List <MappingPropertiesPair> mappingProperties = GetMappingProperties(mappingEntryInfo); mappingProperties.AddRange(_mapperConfiguration.GetRegisteredMappings <TSource, TDestination>()); result = _mappingFunctionsFactory.CreateMappingFunction <TSource, TDestination>(mappingProperties); _mappingFunctionsCache.AddToCache(mappingEntryInfo, result); } return(result); }
public void AddToCache <TSource, TDestination>(MappingTypesPair mappingEntryInfo, Func <TSource, TDestination> mappingFunction) { _cache.Add(mappingEntryInfo, mappingFunction); }
public bool HasCacheFor(MappingTypesPair mappingEntryInfo) => _cache.ContainsKey(mappingEntryInfo);
public Func <TSource, TDestination> GetCacheFor <TSource, TDestination>(MappingTypesPair mappingEntryInfo) => (Func <TSource, TDestination>)_cache[mappingEntryInfo];
// Internals protected bool Equals(MappingTypesPair other) { return(Source == other.Source && Destination == other.Destination); }