Example #1
0
        internal static bool ResolvePropertyExpressionOrCriteria(object value, ModelPropertyInfo argumentProperty, WhereBuilderContext context)
        {
            var method = context.Arguments.GetType().GetMethod("Resolve" + argumentProperty.Name,
                                                               BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { argumentProperty.Type }, null);

            if (method != null)
            {
                Type retType = typeof(Expression <>).MakeGenericType(typeof(Func <,>).MakeGenericType(context.ElementType, typeof(bool)));

                if (method.ReturnType == retType)
                {
                    LambdaExpression lambda = (LambdaExpression)method.Invoke(context.Arguments, new object[] { value });

                    if (lambda != null)
                    {
                        //ExpressionReplacementUtil util = new ExpressionReplacementUtil(lambda.Parameters[0], context.Parameter);

                        //context.Append(util.Visit(lambda.Body));

                        context.Append(ReplacementVisitor.Replace(lambda.Body, lambda.Parameters[0], context.Parameter));
                    }

                    return(true);
                }
                else if ((typeof(Criteria)).IsAssignableFrom(method.ReturnType))
                {
                    Criteria criteria = (Criteria)method.Invoke(context.Arguments, new object[] { value });

                    if (criteria != null)
                    {
                        ResolvePropertyDefault(criteria, argumentProperty, context);
                    }

                    return(true);
                }
            }

            return(false);
        }
Example #2
0
        private static void ResolvePropertyDefault(object value, ModelPropertyInfo mp, WhereBuilderContext context)
        {
            Criteria criteria = value as Criteria;

            if (criteria == null)
            {
                criteria = Criteria.Create(value, mp);
            }

            var members = MapProperty(mp, context);

            if (members != null)
            {
                Expression expr = null;

                foreach (var mb in members)
                {
                    var expr1 = criteria.CreateExpression(mb, context);

                    if (expr1 != null)
                    {
                        if (expr == null)
                        {
                            expr = expr1;
                        }
                        else
                        {
                            expr = Expression.OrElse(expr, expr1);
                        }
                    }
                }

                if (expr != null)
                {
                    context.Append(expr);
                }
            }
        }