NHibernate中的表达树 [英] Expression Trees in NHibernate

查看:58
本文介绍了NHibernate中的表达树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有此签名的方法

I have a method which have this signature

public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)

我过去一直通过lambda表达式,并通过从expressiontree中检索数据来在nhibernate中进行搜索限制.

I use to pass lambda expressions and make search restriction in nhibernate by retrieving data from expressiontree.

因此,当类用户传递类似以下内容的内容时:

So when class user pass something like :

c => c.fullName == "John" && c.lastName == "Smith" && c.lastName != "" || c.fullName != ""  && c.Age > 18

我可以从表达式树中读取此结构,这样我就可以提供完整的智能感知方式来提供搜索条件

I get to read this structure from expression tree, that way I have a full intellisense way to provide searching criteria

换句话说:我需要将搜索条件传递给数据访问层(Nhibernate)

In other words: I need to pass searching criteria to data access layer (Nhibernate)

因此,我需要从表达式树中提取条件,然后通过示例将其传递给n休眠状态:

So I need to extract criteria from expression tree and then pass it to n hibernate by example :

c=>c.fullname = "John" 

我将从表达式树中提取以下信息:

I will extract the following information from the expression tree :

propertyname = fullname , value = "John" , restrictiontype = "equality" 

,然后将此信息传递给nhibernate,如下所示:

and then pass this info to nhibernate as following :

ICriteria crit = session.CreateCriteria(typeof(T));
                    crit.Add(Restrictions.Eq(propretyName, value));
    IList<T> list = crit.Add(List<T>())
                    return list;

无论如何,问题是很难从expressiontree读取数据,所以我想知道你们是否有任何简单的方法,也许在expressiontree中进行迭代以提取数据,或者你们是否有一些代码可以从ExpressionTree检索数据. /p>

Any way the problem is it's really hard to read from expressiontree, so I was wondering if you guys have any easy way of maybe iterating inside expressiontree to pull data, or maybe you guys have some code to retrieve data from ExpressionTree.

推荐答案

以下是一些代码,用于检索您提到的信息.我确定您可以扩展此范围,以包括可能需要的其他信息.

Here is some code that retrieves the information you mentioned. I’m sure you can extend this to include additional information you might be looking for.

public class Criterion
{
    public string PropertyName;
    public object Value;
    public ExpressionType RestrictionType;
}

[....]

public static IEnumerable<Criterion> GetCriteria<T>(Expression<Func<T, bool>> expression)
{
    return getCriteria<T>(expression.Body);
}
private static IEnumerable<Criterion> getCriteria<T>(Expression expression)
{
    if (expression is BinaryExpression)
    {
        var bin = (BinaryExpression) expression;
        if (bin.NodeType == ExpressionType.And || bin.NodeType == ExpressionType.AndAlso ||
            bin.NodeType == ExpressionType.Or || bin.NodeType == ExpressionType.OrElse)
            return getCriteria<T>(bin.Left).Concat(getCriteria<T>(bin.Right));

        if (bin.Left is MemberExpression)
        {
            var me = (MemberExpression) bin.Left;
            if (!(bin.Right is ConstantExpression))
                throw new InvalidOperationException("Constant expected in criterion: " + bin.ToString());
            return new[] { new Criterion {
                PropertyName = me.Member.Name,
                Value = ((ConstantExpression) bin.Right).Value,
                RestrictionType = bin.NodeType
            } };
        }

        throw new InvalidOperationException("Unsupported binary operator: " + bin.NodeType);
    }

    throw new InvalidOperationException("Unsupported expression type: " + expression.GetType().Name);
}

这篇关于NHibernate中的表达树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆