public Expression CreateMapExpression(Type sourceType, Type destinationType, IDictionary <string, object> parameters = null, params MemberInfo[] membersToExpand)
        {
            parameters = parameters ?? new Dictionary <string, object>();

            var cachedExpression =
                _expressionCache.GetOrAdd(new ExpressionRequest(sourceType, destinationType, membersToExpand),
                                          tp => CreateMapExpression(tp, new ConcurrentDictionary <ExpressionRequest, int>()));

            if (!parameters.Any())
            {
                return(cachedExpression);
            }

            var visitor = new ConstantExpressionReplacementVisitor(parameters);

            return(visitor.Visit(cachedExpression));
        }
        /// <summary>
        /// Create an expression tree representing a mapping from the <typeparamref name="TSource"/> type to <typeparamref name="TDestination"/> type
        /// Includes flattening and expressions inside MapFrom member configuration
        /// </summary>
        /// <typeparam name="TSource">Source Type</typeparam>
        /// <typeparam name="TDestination">Destination Type</typeparam>
        /// <param name="mappingEngine">Mapping engine instance</param>
        /// <param name="parameters">Optional parameter object for parameterized mapping expressions</param>
        /// <param name="membersToExpand">Expand members explicitly previously marked as members to explicitly expand</param>
        /// <returns>Expression tree mapping source to destination type</returns>
        public static Expression <Func <TSource, TDestination> > CreateMapExpression <TSource, TDestination>(
            this IMappingEngine mappingEngine, System.Collections.Generic.IDictionary <string, object> parameters = null, params string[] membersToExpand)
        {
            //Expression.const
            parameters = parameters ?? new Dictionary <string, object>();

            var cachedExpression = (Expression <Func <TSource, TDestination> >)
                                   _expressionCache.GetOrAdd(new ExpressionRequest(typeof(TSource), typeof(TDestination), membersToExpand),
                                                             tp => CreateMapExpression(mappingEngine, tp, DictionaryFactory.CreateDictionary <ExpressionRequest, int>()));

            if (!parameters.Any())
            {
                return(cachedExpression);
            }

            var visitor = new ConstantExpressionReplacementVisitor(parameters);

            return((Expression <Func <TSource, TDestination> >)visitor.Visit(cachedExpression));
        }
Exemple #3
0
        private Expression Prepare(Expression cachedExpression, ParameterBag parameters)
        {
            Expression result;

            if (parameters.Any())
            {
                var visitor = new ConstantExpressionReplacementVisitor(parameters);
                result = visitor.Visit(cachedExpression);
            }
            else
            {
                result = cachedExpression;
            }
            // perform null-propagation if this feature is enabled.
            if (_configurationProvider.EnableNullPropagationForQueryMapping)
            {
                var nullVisitor = new NullsafeQueryRewriter();
                return(nullVisitor.Visit(result));
            }
            return(result);
        }
        public Expression CreateMapExpression(Type sourceType, Type destinationType, IDictionary <string, object> parameters = null, params MemberInfo[] membersToExpand)
        {
            parameters = parameters ?? new Dictionary <string, object>();

            var cachedExpression = _expressionCache.GetOrAdd(new ExpressionRequest(sourceType, destinationType, membersToExpand, null));

            Expression x = cachedExpression;

            if (parameters.Any())
            {
                var visitor = new ConstantExpressionReplacementVisitor(parameters);
                x = visitor.Visit(cachedExpression);
            }

            // perform null-propagation if this feature is enabled.
            if (_configurationProvider.EnableNullPropagationForQueryMapping)
            {
                var nullVisitor = new NullsafeQueryRewriter();
                return(nullVisitor.Visit(x));
            }
            return(x);
        }