NHibernate: выяснение, сопоставлено ли свойство с полем - PullRequest
3 голосов
/ 13 мая 2009

Есть ли способ узнать, сопоставлено ли свойство с полем. Я хотел бы, чтобы это генерировало что-то вроде «общего поиска»:

    string[] words.
    words = search.Split(' ');
    Type type = typeof(T);

    Disjunction disjunction = new Disjunction();
    foreach (System.Reflection.PropertyInfo property in type.GetProperties())
    {
        if ((property.PropertyType == typeof(string)))
        {

            foreach (string word in words)
            {
                disjunction.Add(
                    Expression.InsensitiveLike(
                        property.Name,
                        "%" + word + "%"));
            }
        }
    }

Если я добавлю свойство, которое не сопоставлено с NHibernate, поиск выдаст исключение NHibernate.QueryException с описанием «не удалось разрешить свойство: Text1 of: C»

Я сопоставляю свойства следующим образом:

class C
{    
    [Property(0, Column = "comment")]
    public virtual string Comment {get; set;}
}

1 Ответ

4 голосов
/ 13 мая 2009

Используйте API метаданных NHibernate.

ISessionFactory sessionFactory;

Type type = typeof(T);
IClassMetadata meta = sessionFactory.GetClassMetadata(type);

Disjunction disjunction = new Disjunction();
foreach (string mappedPropertyName in meta.PropertyNames)
{
    IType propertyType = meta.GetPropertyType(mappedPropertyName);

    if (propertyType == NHibernateUtil.String)
    {
        foreach (string word in words)
        {
            disjunction.Add(
                Expression.InsensitiveLike(
                    mappedPropertyName,
                    "%" + word + "%"));
        }
    }
}
...