Example #1
0
            public static MappingCompiledInfo CompileMapping(
                Mapper mapper,
                MapperContext context,
                Mapping mapping,
                MappingType mappingType)
            {
                _ensureCompilers();

                var result = new MappingCompiledInfo();

                var compilerContext = new Context(mapper, context, mapping, result);

                var source = Expression.Parameter(mapping.SourceType, "source");

                switch (mappingType)
                {
                case MappingType.ObjectMap:
                {
                    break;
                }

                case MappingType.QueryableProjection:
                {
                    foreach (var compiler in Compilers)
                    {
                        if (compiler.CanMap(compilerContext, mapping.SourceType, mapping.DestinationType))
                        {
                            var exp = compiler.CreateConvertExpression(
                                compilerContext,
                                source,
                                mapping.DestinationType,
                                mappingType
                                );

                            result.ProjectionExpression = Expression.Lambda(exp, source);
                        }
                    }

                    break;
                }

                case MappingType.CollectionSynchronization:
                {
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException(nameof(mappingType), mappingType, null);
                }

                if (result == null)
                {
                    throw new ArgumentOutOfRangeException(nameof(mappingType), mappingType, null);
                }

                return(result);
            }
Example #2
0
        public bool CacheCompiledInfo(Type destinationType, Type sourceType, MappingCompiledInfo mappingCompiledInfo)
        {
            var hash = Mapping.ComputeHash(destinationType, sourceType);

            Lock.EnterWriteLock();

            try
            {
                return(CompiledCache.Cache(hash, mappingCompiledInfo));
            }
            finally
            {
                Lock.ExitWriteLock();
            }
        }
Example #3
0
        /// <summary>
        /// Returns the <see cref="MappingCompiledInfo"/> using given destinationType and sourceType.
        /// </summary>
        /// <param name="destinationType"></param>
        /// <param name="sourceType"></param>
        /// <param name="mappingCompiledInfo"></param>
        /// <returns>A value indicating that value is found or not.</returns>
        public bool TryGetCompiledInfo(Type destinationType, Type sourceType,
                                       out MappingCompiledInfo mappingCompiledInfo)
        {
            var hash = Mapping.ComputeHash(destinationType, sourceType);

            Lock.EnterReadLock();

            try
            {
                return(CompiledCache.TryGetValue(hash, out mappingCompiledInfo));
            }
            finally
            {
                Lock.ExitReadLock();
            }
        }
Example #4
0
        public bool TryGetMappingAndCompiledInfo(Type destinationType, Type sourceType, out Mapping mapping,
                                                 out MappingCompiledInfo mappingCompiledInfo)
        {
            var hash = Mapping.ComputeHash(destinationType, sourceType);

            Lock.EnterReadLock();

            try
            {
                var result = MappingRows.TryGetValue(hash, out mapping);
                CompiledCache.TryGetValue(hash, out mappingCompiledInfo);
                return(result);
            }
            finally
            {
                Lock.ExitReadLock();
            }
        }