Note that there are some explanatory texts on larger screens.

plurals
  1. POFetching strategy encapsulation for Entity Framework 4.1 and NHibernate
    primarykey
    data
    text
    <p>I created a project to test out NHibernate 3+ vs. Entity Framework 4.1, wrapping it in a repository, making it very testable using interfaces etc.</p> <p>I do not want to expose either ORM outside of the repositories (I do not even expose IQueryables). Everything should be handled in that layer and until I tried to handle fetching in an abstract way, everything was good.</p> <p>Microsoft's implementation of adding eager loading uses either magic strings (yuck) or Linq expressions (yay) on the Include function. Their syntax follows something like this:</p> <pre><code>IQueryableThing.Include(o =&gt; o.Person); IQueryableThing.Include(o =&gt; o.Company.Contact); IQueryableThing.Include(o =&gt; o.Orders.Select(p =&gt; p.LineItem.Cost); </code></pre> <p>The first will just load the associated person. (parent) The second will load the associated company and each company's contact. (parent and grandparent). The third will load all associated orders, line items and costs for each order.</p> <p>It's a pretty slick implementation.</p> <p>NHibernate uses a slightly different approach. They still use Linq expressions, but they make heavier use of extension methods (fluent approach).</p> <pre><code>IQueryableThing.Fetch(o =&gt; o.Person); IQueryableThing.Fetch(o =&gt; o.Company).ThenFetch(o =&gt; o.Contact); IQueryableThing.FetchMany(o =&gt; o.Orders).ThenFetch(p =&gt; p.LineItem).ThenFetch(q =&gt; q.Cost); </code></pre> <p>(I'm not sure I if the third line is the correct syntax)</p> <p>I can encapsulate a list of expressions in a separate class and then apply those expression to the IQueryable within that class. So what I would need to do is standardize on the Microsoft expression syntax and then translate that into NHibernate's syntax by walking the expression tree and rebuilding each expression.</p> <p>This is the part that's really tricky. I have to maintain a particular order of operations in order to call the correct function for the IQueryable (must start with either Fetch or FetchMany, with each subsequent being a "ThenFetch" or "ThenFetchMany"), which stops me from using the built-in ExpressionVisitor class.</p> <p>Edit: I finally created an expression parser that will take any level of nesting of properties, collections, and selects on collections and produce an array of expressions. Unfortunately, the built in Fetch extensions methods do not take LambdaExpression as a parameter.</p> <p><strong>The part I am stuck on currently</strong> is not being able to use the built in Fetch definitions from nHibernate. It looks like I may have to hit the Remotion library's functions directly or register my own extension methods that will satisfy their parser.</p> <p>Funky.</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