The NHibernate optimization debate: partial object queries

Aaron (Eleutian) and Oren (Ayende) have a little backandforth going about optimizing NHibernate. I’m no expert on NHibernate, but I do have experience with O/R mappers and even more experience with raw databases and SQL, and on techniques for improving performance on both, so I feel I could chime in at least in one area of their debate…

Partial object queries

Let me bring you up to date:

  • Aaron wants to run an NHibernate query of select u(Username, Email) from User u and get a User object with only those two fileds filled in.
  • Oren says you can do it: select new UserSummaryDetails(u.Username, u.Email) from User u.
  • Aaron disputes that as a solution, since it returns a UserSummaryDetails object and not a User object, which would require him to rewrite methods that work on a User object but only use a subset of fields (he mentions a SendMailTo method which uses only the username and email).

There’s a solution that perhaps can perhaps make both happy. You specify an interface (IEmailInfo) that exposes the Username and Email properties, implement that interface in your User and UserSummaryDetails classes, and use the interface on your SendMailTo method? At least, this seems plausible considering my limited (but growing) NHibernate exposure, and would work in other O/R mappers (like Paul Wilson’s).

Either way, keeping up with the aforementioned blog debate is a good way to understand the inner workings of NHibernate.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>