protected Tuple <List <Expression>, ParameterExpression, ParameterExpression> GetMapExpressions(Type src, Type dest)
        {
            var cacheKey = MappingServiceProvider.CalculateCacheKey(src, dest);

            if (TypeMappers.ContainsKey(cacheKey))
            {
                return(TypeMappers[cacheKey].GetMapExpressions());
            }
            throw new MapNotImplementedException(string.Format("There is no mapping has bee found. Source Type: {0}, Destination Type: {1}", src.FullName, dest.FullName));
        }
Esempio n. 2
0
        protected BlockExpression GetCustomMapExpression(Type src, Type dest)
        {
            var cacheKey = MappingServiceProvider.CalculateCacheKey(src, dest);

            if (!MappingServiceProvider.CustomMappers.ContainsKey(cacheKey))
            {
                return(null);
            }
            CompileGenericCustomTypeMapper(src, dest, MappingServiceProvider.CustomMappers[cacheKey](), cacheKey);
            return(CustomTypeMapperExpCache[cacheKey]);
        }
Esempio n. 3
0
        public override void PrecompileCollection <T, TN>()
        {
            var cacheKey = MappingServiceProvider.CalculateCacheKey(typeof(T), typeof(TN));

            if (CollectionMappers.ContainsKey(cacheKey))
            {
                return;
            }

            var sourceParameterExp = Expression.Parameter(typeof(T), "sourceColl");
            var blockExp           = CompileCollectionInternal <T, TN>(sourceParameterExp);
            var lambda             = Expression.Lambda <Func <T, TN> >(blockExp, sourceParameterExp);
            var compiledFunc       = lambda.Compile();

            CollectionMappers[cacheKey] = compiledFunc;
        }
Esempio n. 4
0
        public Expression GetMemberQueryableExpression(Type srcType, Type dstType)
        {
            var cacheKey = MappingServiceProvider.CalculateCacheKey(srcType, dstType);

            if (!TypeMappers.ContainsKey(cacheKey))
            {
                return(null);
            }

            var typeMapper = TypeMappers[cacheKey];

            if (typeMapper.QueryableGeneralExpression == null)
            {
                typeMapper.Compile(CompilationTypes.Source);
            }
            return(typeMapper.QueryableGeneralExpression);
        }
Esempio n. 5
0
        public override void PrecompileCollection <T, TN>()
        {
            var cacheKey = MappingServiceProvider.CalculateCacheKey(typeof(T), typeof(TN));

            if (CollectionMappers.ContainsKey(cacheKey))
            {
                return;
            }

            var sourceType = GetCollectionElementType(typeof(T));
            var destType   = GetCollectionElementType(typeof(TN));

            var sourceVariable = Expression.Parameter(typeof(T), "source");
            var destVariable   = Expression.Parameter(typeof(TN), "dst");

            var srcCount  = Expression.Call(typeof(Enumerable), "Count", new[] { sourceType }, sourceVariable);
            var destCount = Expression.Call(typeof(Enumerable), "Count", new[] { destType }, destVariable);

            var conditionToCreateList = Expression.NotEqual(srcCount, destCount);
            var notNullCondition      = Expression.IfThenElse(conditionToCreateList,
                                                              MapCollectionNotCountEquals(typeof(T), typeof(TN), sourceVariable, destVariable),
                                                              MapCollectionCountEquals(typeof(T), typeof(TN), sourceVariable, destVariable));

            var newCollBlockExp = CompileCollectionInternal <T, TN>(sourceVariable, destVariable);

            var result = Expression.IfThenElse(Expression.NotEqual(destVariable, StaticExpressions.NullConstant),
                                               notNullCondition,
                                               newCollBlockExp);

            var expressions = new List <Expression> {
                result
            };

            var resultExpression = Expression.Block(new ParameterExpression[] {}, expressions);

            var checkSrcForNullExp =
                Expression.IfThenElse(Expression.Equal(sourceVariable, StaticExpressions.NullConstant),
                                      Expression.Default(destVariable.Type), resultExpression);
            var block        = Expression.Block(new ParameterExpression[] {}, checkSrcForNullExp, destVariable);
            var lambda       = Expression.Lambda <Func <T, TN, TN> >(block, sourceVariable, destVariable);
            var compiledFunc = lambda.Compile();

            CollectionMappers[cacheKey] = compiledFunc;
        }
Esempio n. 6
0
        protected Tuple <List <Expression>, ParameterExpression, ParameterExpression> GetMapExpressions(Type src, Type dest)
        {
            var cacheKey = MappingServiceProvider.CalculateCacheKey(src, dest);

            if (TypeMappers.ContainsKey(cacheKey))
            {
                return(TypeMappers[cacheKey].GetMapExpressions());
            }

            dynamic srcInst  = Activator.CreateInstance(src);
            dynamic destInst = Activator.CreateInstance(dest);

            RegisterDynamic(srcInst, destInst);
            if (TypeMappers.ContainsKey(cacheKey))
            {
                return(TypeMappers[cacheKey].GetMapExpressions());
            }

            throw new MapNotImplementedException(
                      $"There is no mapping has been found. Source Type: {src.FullName}, Destination Type: {dest.FullName}");
        }
 private void RegisterDynamic <T, TN>(T src, TN dest)
 {
     MappingServiceProvider.Register <T, TN>();
 }