Esempio n. 1
0
        public TDestination Map <TSource, TDestination>(TSource source) where TDestination : new()
        {
            var mappingTypes    = new MappingTypes(typeof(TSource), typeof(TDestination));
            var mappingFunction = GetOrCreateMappingFunction <TSource, TDestination>(mappingTypes);

            return(mappingFunction(source));
        }
Esempio n. 2
0
        private void AddCongiguration(MappingTypes typesMapping, MappingProperties propertiesMapping)
        {
            List <MappingProperties> registeredMappings;

            if (!_configuratedMappings.TryGetValue(typesMapping, out registeredMappings))
            {
                registeredMappings = new List <MappingProperties>();
                _configuratedMappings[typesMapping] = registeredMappings;
            }
            registeredMappings.Add(propertiesMapping);
        }
Esempio n. 3
0
        internal IEnumerable <MappingProperties> GetRegisteredMappings <TSource, TDestination>()
        {
            var mappingEntryInfo = new MappingTypes(typeof(TSource), typeof(TDestination));
            List <MappingProperties> registeredMappings;

            if (!_configuratedMappings.TryGetValue(mappingEntryInfo, out registeredMappings))
            {
                registeredMappings = new List <MappingProperties>();
            }
            return(registeredMappings);
        }
Esempio n. 4
0
 private static List <MappingProperties> GetMappingProperties(MappingTypes mappingTypes)
 {
     return((
                from sourceProperty in mappingTypes.Source.GetProperties()
                join destinationProperty in mappingTypes.Destination.GetProperties()
                on sourceProperty.Name equals destinationProperty.Name
                let mappingProperties = new MappingProperties(sourceProperty, destinationProperty)
                                        where CanMapProperties(mappingProperties)
                                        select mappingProperties
                ).ToList());
 }
Esempio n. 5
0
        public MapperConfiguration Register <TSource, TDestination>(
            Expression <Func <TSource, object> > sourcePropertyAccessor,
            Expression <Func <TDestination, object> > destinationPropertyAccessor)
        {
            MappingProperties addingPropertiesMapping = GetAddingsMapping(sourcePropertyAccessor,
                                                                          destinationPropertyAccessor);
            var typesMapping = new MappingTypes(typeof(TSource), typeof(TDestination));

            if (Mapper.CanMapProperties(addingPropertiesMapping))
            {
                AddCongiguration(typesMapping, addingPropertiesMapping);
            }

            return(this);
        }
Esempio n. 6
0
        private Func <TSource, TDestination> GetOrCreateMappingFunction <TSource, TDestination>(MappingTypes mappingTypes) where TDestination : new()
        {
            Delegate cachedFunction;
            Func <TSource, TDestination> mappingFunction;
            var hasCachedFunction = _mappedTypesFunctions.TryGetValue(mappingTypes, out cachedFunction);

            if (hasCachedFunction)
            {
                mappingFunction = (Func <TSource, TDestination>)cachedFunction;
            }
            else
            {
                var mappingProperties = GetMappingProperties(mappingTypes);
                mappingProperties.AddRange(_configuration.GetRegisteredMappings <TSource, TDestination>());
                mappingFunction = CreateMappingFunction <TSource, TDestination>(mappingProperties);
                _mappedTypesFunctions.Add(mappingTypes, mappingFunction);
            }
            return(mappingFunction);
        }
Esempio n. 7
0
 internal static bool CanAssign(MappingTypes typesMapping)
 {
     return(CanAssign(typesMapping.Source, typesMapping.Destination));
 }