Tuesday, September 02, 2008

What's up with Linq to NHibernate?

With my current clients I'm building an MVC Framework application using NHibernate. I love NHibernate. Like all the best frameworks it just works for most common scenarios. I'm a new user, but I've been able to get up to speed with it very quickly. The power and flexibility of the mapping options make it easy to use with the legacy database that we're lumbered with. But the best thing about it though, is that it's built from the ground up for Domain Driven Development. This means it's possible to build a finely grained domain model unencumbered by data access concerns.

Seriously, if you're building .NET enterprise level business software and you haven't considered using NHibernate (or a competing ORM) you missing out on a huge productivity win.

Any ORM targeting .NET has to support Linq. Here the NHibernate story's not so good at the moment. The coding phenomena that is Ayende kicked off the first Linq to NHibernate implementation a while back. It was originally part of his Rhino Tools project and layered the Linq implementation on top of the NHibernate criteria API. This has now been moved to the NHibernate contrib project. The source is here:


I've been using this in my current project, it works but only with the most straightforward queries. Anything tricky tends to fail and I've tended to go through a process of trial and error to find the right patterns. The problem seems to be mostly around the criteria API. It doesn't support a lot of the features that Linq requires, especially nested queries.

Recently a new NHibernate.Linq project has been started in the NHibernate trunk that takes a different approach. The authors are building SQL directly from the expression tree which obviously gives a lot more flexibility. The intention is to also to convert HQL to an expression tree thus having a single uniform query translation.

This is very exciting for NHibernate but there's going to be a hiatus while the new implementation evolves.

Thanks to Tuna Toksöz for the info.


Anonymous said...

Meanwhile I'm very happy with HQL and the Criteria API.
I really see no great merit in Linq for NH, besides making it a little more client-sexyeable

Mike Hadlow said...

I've got to dissagree with you there Ken, the potential of Linq is huge and I think the community is only at the first stages of working out what it all means. The core message I guess is unification of in-memory querying vs db querying, allowing the same natural declarative syntax whatever your backing store. Any .NET ORM really has to support it.

preet sangha said...

Someone from MS mentioned at Tech Ed yesterday that he was looking at NHibernate wrt to Linq. Couldn't say anymore as I wasn't up to speed with what the current state of Linq 2 NHibernate is.

And to answer Ken - I agree with Mike. There is a lot more to linq than syntactic sugar. I see it as more about querying at appropriate service layers in a client agnostic way.