NHibernate克服NotSupportedException [英] NHibernate Overcoming NotSupportedException
问题描述
有人知道有什么方法可以克服NotSupportedException吗?我有一个针对用户的方法:
Does anyone know of any way to overcome NotSupportedException? I have a method against a User:
public virtual bool IsAbove(User otherUser)
{
return HeirarchyString.StartsWith(otherUser.HeirarchyString);
}
我想做
_session.Query<User>.Where(x => loggedInUser.IsAbove(x));
但这会引发NotSupportedException.但是真正的痛苦是使用
But this throws a NotSupportedException. The real pain though is that using
_session.Query<User>.Where(x => loggedInUser.HeirarchyString.StartsWith(x.HeirarchyString));
绝对正常.但是,我不喜欢将其作为解决方案,因为这意味着,如果我更改IsAbove方法的工作方式,则必须记住要更新代码的所有重复位置
works absolutely fine. I don't like this as a solution, however, because it means that if I change how the IsAbove method works, I have to remember all the places where I have duplicated the code whenever I want to update it
推荐答案
命名规范表达式并重复使用,例如:
Name the specification expression and reuse that, e.g:
public Expression<Func<....>> IsAboveSpecification = (...) => ...;
public virtual bool IsAbove(User otherUser)
{
return IsAboveSpecification(HeirarchyString, otherUser.HeirarchyString);
}
根据需要在查询中重用IsAboveSpecification.如果经常使用IsAbove()方法,则可以在表达式上缓存Compile()方法的结果.
Reuse IsAboveSpecification in the query as needed. If the IsAbove() method is used often use can cache the result of the Compile() method on the expression.
这篇关于NHibernate克服NotSupportedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!