Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I wrap Linq2NHibernate's .Fetch and .ThenFetch inside my abstract repository?
    primarykey
    data
    text
    <p>I'm using a generic repository that exposes an <code>IQueryable&lt;T&gt;</code> like this:</p> <pre><code>public IQueryable&lt;T&gt; AllEntities { get { return session.Query&lt;T&gt;(); } } </code></pre> <p>I can query like this:</p> <pre><code>var results = (from e in repository.AllEntities where e.SomeProperty == "some value" select e).ToList(); </code></pre> <p>However, if <code>T</code> has a parent and grandparent entity and I want to load them eagerly, I have to do this:</p> <pre><code>var results = (from e in repository.AllEntities where e.SomeProperty == "some value" select e) .Fetch(x =&gt; x.Parent) .ThenFetch(x =&gt; x.Grandparent) .ToList(); </code></pre> <p>This works, but <code>.Fetch</code> and <code>.ThenFetch</code> are both Linq2Nhibernate specific extension methods, which is causing two problems:</p> <ol> <li><p>I have to include a <code>using NHibernate.Linq;</code> statement at the top of my file. However, at the point that I'm doing this query, it should be implementation agnostic.</p></li> <li><p>When I try to unit test this, the <code>.Fetch</code> and <code>.ThenFetch</code> methods fail when executed against the <code>IQueryable&lt;T&gt;</code> that my mock repository provides.</p></li> </ol> <p>How can I wrap these inside of my <code>IRepository&lt;T&gt;</code> interface, or inside of some generic extension methods?</p> <p><strong>Update:</strong></p> <p>So far all I've come up with is to add this to my repository interface:</p> <pre><code>IQueryable&lt;T&gt; EagerLoadParent&lt;U&gt;(IQueryable&lt;T&gt; query, Expression&lt;Func&lt;T, U&gt;&gt; parentExpression); IQueryable&lt;T&gt; EagerLoadParent&lt;U, V&gt;(IQueryable&lt;T&gt; query, Expression&lt;Func&lt;T, U&gt;&gt; parentExpression, Expression&lt;Func&lt;U, V&gt;&gt; grandparentExpression); </code></pre> <p>... and this to my NHibernate repository implementation:</p> <pre><code>public IQueryable&lt;T&gt; EagerLoadParent&lt;U&gt;(IQueryable&lt;T&gt; query, Expression&lt;Func&lt;T, U&gt;&gt; parentExpression) { return query .Fetch(parentExpression); } public IQueryable&lt;T&gt; EagerLoadParent&lt;U, V&gt;(IQueryable&lt;T&gt; query, Expression&lt;Func&lt;T, U&gt;&gt; parentExpression, Expression&lt;Func&lt;U, V&gt;&gt; grandparentExpression) { return query .Fetch(parentExpression) .ThenFetch(grandparentExpression); } </code></pre> <p>The consumer of this API now does this:</p> <pre><code>var query = (from e in repository.AllEntities where e.SomeProperty == "some value" select e); var results = repository .EagerLoadParent(query, e =&gt; e.Parent, p =&gt; p.Grandparent) .ToList(); </code></pre> <p>But this lacks the nice extension method syntax I'd prefer. I'm looking for something closer to the <code>.Fetch</code> and <code>.ThenFetch</code> syntax.</p>
    singulars
    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