3

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.

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.

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:

IQueryableThing.Include(o => o.Person);
IQueryableThing.Include(o => o.Company.Contact);
IQueryableThing.Include(o => o.Orders.Select(p => p.LineItem.Cost);

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.

It's a pretty slick implementation.

NHibernate uses a slightly different approach. They still use Linq expressions, but they make heavier use of extension methods (fluent approach).

IQueryableThing.Fetch(o => o.Person);
IQueryableThing.Fetch(o => o.Company).ThenFetch(o => o.Contact);
IQueryableThing.FetchMany(o => o.Orders).ThenFetch(p => p.LineItem).ThenFetch(q => q.Cost);

(I'm not sure I if the third line is the correct syntax)

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.

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.

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.

The part I am stuck on currently 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.

Funky.

junkyspace
  • 365
  • 4
  • 15

1 Answers1

0

Have you tried using NHiberanteUtil.Initialize()? I haven't attempted to do what you are doing, but I think Initialize will work akin to Include().

Steve Mallory
  • 4,245
  • 1
  • 28
  • 31
  • I would like to expose a single approach to applying eager loading outside of the persistence layer without exposing the details of the ORM itself. So I am using Linq expressions and need to parse the Linq expressions and supply them to nHibernate's fetch methods. Please explain further how Initialize resolves this, because I do not see it. – junkyspace May 27 '11 at 14:10
  • @junkyspace Well I'm not actually sure it will resolve it, but Initialize accepts something like `NHibernateUtil.Initialize(IQueryableThing.Fetch(o=>o.Company.Contact))`. My idea is that you would use the same expression. – Steve Mallory May 27 '11 at 15:02
  • @junkyspace Nevermind. What I'm suggesting runs, but doesn't actually do the eager load. If I try to force it, with FirstOfDefault(), Fetch() throws an exception - expression too complex. – Steve Mallory May 27 '11 at 15:13
  • Thanks for trying :) I ran into the same problem just specifying it in the fetch. I have it half worked out right now with parent/grandparent relationships (o => o.parent1.parent1), but the dissecting one-to-many (parent.collection.select(o => o.something)) expression is a bit tricky. – junkyspace May 27 '11 at 20:45