NHibernate Overcoming NotSupportedException

谁都会走 提交于 2019-12-21 12:34:16

问题


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);
 }

And I want to do:

_session.Query<User>.Where(x => loggedInUser.IsAbove(x));

But this throws a NotSupportedException. The real pain though is that using

_session.Query<User>.Where(x => loggedInUser.HeirarchyString.StartsWith(x.HeirarchyString));

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


回答1:


Name the specification expression and reuse that, e.g:

public Expression<Func<....>> IsAboveSpecification = (...) => ...;

public virtual bool IsAbove(User otherUser)
{
    return IsAboveSpecification(HeirarchyString, otherUser.HeirarchyString);
}

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.



来源:https://stackoverflow.com/questions/11079852/nhibernate-overcoming-notsupportedexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!