Question

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)

Was it helpful?

Solution

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. :-)

OTHER TIPS

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.

Start with NHibernate is a bad idea. It shows a good performance only with ably settings. Try to use EFv4 for large projects and L2S (maybe 3rd-part products) for small and medium size. These products are more convenient and flexible than NHibernate and allow you to start quickly.

not a complete list

LinqToSQL Pro:

  • better tool support
  • good linq provider
  • easy to start with when db-schema == classes -

Con:

  • not flexible (ie db-schema != classes)
  • only supports MS SQL Server
  • no cascading (save, update ... doesnt cascade to referenced objects)

NHibernate Pro:

  • a lot rdbms supported ootb
  • feature rich
  • very flexible for almost all corner cases
  • open source

Con:

  • not so easy to start with
  • not from MS
  • there are many tools, but you have to search for

Between the 2 ORMs

i would choose LinqToSql if:

  • db-schema == classes
  • only ever use MS SQL Server
  • shop only allows MS-Products

i would choose Nhibernate if:

  • richer objectmodel
  • legacy db-schema
  • DB other than MS SQL Server or support multiple
  • performance critical (i think NH has more features to optimise performance than LinqToSql)

NOTE: this is my personal view. I deal mostly with (crazy) legacy dbs and complex ETL jobs where the object model helps a lot over SQL.

I don't use (or even know) NHibernate, I just want to give my testimony: I use LINQ to SQL since about 2 years with MySQL and PostgreSQL databases (using DbLinq on Windows, using Mono on Linux and Mac OS X).

So LINQ to SQL is NOT limited to Microsoft products.

I can confirm that LINQ to SQL is very well suited for small and medium projects, or large projects where you have the absolute control of the database structure. As the reviews indicate, LINQ to SQL has some limitations that make it an inappropriate tool when there is no direct mapping between the database tables and the entity classes.

Note : LINQ to SQL doesn't support many-to-many relationships (but this can be easily achieved with a few code lines).

The main drawback of NHibernate is the inability to make use of method calls. They cannot be translated to SQL. To circumvent that, you have to recreate expression trees which is difficult to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top