Friday, April 04, 2008 11:08 PM
Geoff
Linq to SQL vs. NHibernate
It finally clicked with me this afternoon while watching Luca Bolognese demonstrate Linq to SQL to the EMEA crowd.
Linq to SQL (L2S from here on) is about the data. NHibernate is about the domain. At least as far as I understand.
In the demo, Luca at first creates what would appear to the naked eye to be a domain object, but it's really just a very decent wrapper class DTO style object. He then goes on to say if you were doing anything remotely serious with L2S you wouldn't work that way.
He then shows the "proper" way to do it, which is to take your database schema and drop it into the L2S classes designer. The designer wires everything up relation wise and you can even throw stored procedures in there. At the end of the day, however, you're just mimicking the database in code. This does not lend itself to the One True Way™.
I want my domain model to be completely ignorant of the database schema supporting it. The domain model should be free to do what it does best (model the domain, ahem), and the ORM should be left to do what it does best: map the domain model to the database using the rules you define.
How does L2S deal with inheritance? Composition? Polymorphism? There will be ways to deal with it, but it's not the intent of Linq. As Anders says, Linq is about stopping programmers being plumbers. I don't want to know the disgusting details of how the data gets from that box over there into objects in my code. I just want it to show up.
NHibernate removes the plumbing, but you still have to be aware of the database. It's still possible in NHibernate, as it will be in L2S, to write code that will bring the database server to it's knees. You need to be aware of this. You also need to be aware of the SQL that it will generate to try to fulfil your requests.
I'm still definitely in the NHibernate camp for serious work, but it's a tough call between ActiveRecord and L2S for me. It looks like a bit less work to set up L2S and get data back than with ActiveRecord.
Also, this doesn't mean you can't use Linq with NHibernate. Quite the opposite. Linq on it's own works wonders with NHibernate. There's some community support around Linq and NHibernate as well, with some code earmarked for the NHibernate libraries coming soon.
This is also not meant to be a L2S bash. Quite the opposite. I'm extremely impressed with what L2S does, and how it does it. I only wrote this post to clarify what I perceive as the differences between L2S and NHibernate.
Go and give it a whack.