Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ to NHibernate - How to detect failed translation
    primarykey
    data
    text
    <p>I'm writing a (yet another) generic repository of entities, which is not necessarily backed by a relational database. I'd like one of the <code>IEnumerable&lt;T&gt; Load&lt;T&gt;(...)</code> methods to take as argument a generic <code>Expression&lt;Func&lt;T, bool&gt;&gt;</code> predicate that specifies user-defined criteria for the entities to retrieve. Please note that I don't want to expose a full <code>IQueryable&lt;T&gt;</code> to the user as I want to limit the exposure of the underlying storage to my users.</p> <p>In those cases in which the repository is backed by NHibernate (3.1, by the way), simple predicates - such as <code>x =&gt; x.Name="Mike"</code> - can be "pushed down" to the relational database by LINQ for NHibernate (using the <code>Where(Expression&lt;Func&lt;T, bool&gt;&gt;)</code> method), with obvious performance gains when the underlying set of entities is large and the predicate only selects one entity. Nice. </p> <p>However, my users do not necessarily know that the repository is backed by a relational database, so the predicates can be at times so complex (e.g. <code>x =&gt; MyFunction(x.Name) == 0</code>) that LINQ to NHibernate fails to generate HQL for them. In these cases I'd like to detect LINQ's failure to generate HQL and transparently "failover" to loading all entities and explicitly applying the predicate to each.</p> <p>The problem is that I cannot find a way to reliably detect that LINQ to NHibernate fails to translate the predicate expression. Executing the query straight away throws a <em>System.NotSupportedException</em> which could be caused by anything, even by an underlying <em>ConnectionProvider</em>. </p> <p>I entertained for a moment the possibility of breaking up the query execution in two - first translate, then execute - and then catch the <em>System.NotSupportedException</em> during translation. To this end, I tried the solution proposed in <a href="https://stackoverflow.com/questions/1327306/does-anyone-know-how-to-translate-linq-expression-to-nhibernate-hql-statement">Does anyone know how to translate LINQ Expression to NHibernate HQL statement?</a> in order to translate the query before execution, and I have to say that I got it to work, but it uses Reflection to access undocumented, non-public methods of internal NHibernate objects, and thus it smells like an unsupported hack. </p> <p>Is there a more reliable and "official" way to either detect that LINQ to NHibernate fails to translate an expression, or to translate the expression without executing the query?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload