views:

2030

answers:

2

If starting a new project what would you use for your ORM NHibernate or LINQ and why. What are the pros and cons of each.

edit: LINQ to SQL not just LINQ (thanks @Jon Limjap)

+8  A: 

Errr... there's LINQ for NHibernate.

Perhaps what you mean is which to use:

  • LINQ to SQL
  • NHibernate

I prefer NHibernate.

LINQ to SQL is fairly lightweight, but it's a little bit more tightly coupled to your data structure, as opposed to NHibernate which is pretty flexible in terms of the types of object definitions that can be mapped to your table structures.

Of course that's not to say that LINQ to SQL has no uses: this very website uses it. I believe that it's quite useful to get up and running in small applications where the database schema is not as massive.

Jon Limjap
+5  A: 

I have asked myself a very similar question except that instead of NHibernate I was thinking about WilsonORM which I have consider pretty nice.

It seems to me that there are many important differences.

LINQ:

  • is not a complete ORM tool (you can get there with some additional libraries like the latest Entity framework - I personally consider the architecture of this latest technology from MS to be about 10 years old when compared with other ORM frameworks)
  • is primarily querying "language" supporting intellisense (compiler will check the syntax of your query)
  • is primarily used with Microsoft SQL Server
  • is closed source

NHibernate:

  • is ORM tool
  • has pretty limited querying language without intellisense
  • can be used with almost any DBMS for which you have a DB provider
  • is open source

It really depends. If you develop a Rich (Windows) desktop application where you need to construct objects, work with them and at the end persist their changes, then I would recommend ORM framework like NHibernate.

If you develop a Web application that usually just query data and only occasionally writes some data back to the DB then I would recommend good querying language like Linq.

So as always, it depends. :-)

David Pokluda
"has pretty limited querying language without intellisense" is kind of misleading... NHibernate has HQL and Criteria APIs. Criteria is regular .NET code so you *do* have IntelliSense. HQL does not have intellisense.
Mauricio Scheffer
"limited querying language": there are very few cases where you can't express a sql query in hql or criteria. Mostly related to CTEs, which also aren't supported in LINQ-to-SQL (http://stackoverflow.com/questions/584841)
Mauricio Scheffer
Finally, there are easy ways to make the Criteria API more strongly typed, see: http://nhforge.org/blogs/149.aspx, http://bugsquash.blogspot.com/2008/03/strongly-typed-nhibernate-criteria-with.html
Mauricio Scheffer
I actually would agree w/ David's point about the querying language. While I wouldn't call it "limiting" per se, I would say that the convergence of experessiveness and intellisense support doesn't match LINQ; Criteria queries still depend on strings out-of-the-box. That said, you can LINQ w/ NHibernate now for many scenarios (not all).
Paul
update: HQL now has intellisense: http://www.felicepollano.com/2010/03/30/HQLEditorWithIntellisense.aspx
Mauricio Scheffer