views:

434

answers:

3

I've been having a brief look at NHibernate and Linq2Sql. I'm also intending to have a peek at Entity Framework.

The question that gets raised, when I talk of these ORM's is "They can't scale", so can they? From Google I get the impression they're able to scale well, but ultimately I suppose there must be a price to pay, Is it worth paying for a richer simpler business layer.

+4  A: 

This is a good question, and IMHO they can scale just as well as any custom DAL. I've only used nHibernate so I will focus only on it and the features it has which can help scale a system.

  • Lazy Loading - Since it supports lazy loading you can avoid loading any unnessecary items. Of course you need to watch out for the Select n+1 problem however there are things in the system to prevent this.
  • Eager Fetching - There are various ways to eagerly fetch objects which you might need allowing you to avoid extra trips to SQL.
  • Second Level Cache - nHibernate has support for a second level cache which can be used to increase the scalability by reducing trips to the DB. There are various backing providers available which give you some flexibility.
  • Write your own SQL - In nHibernate you can call stored procedures, or provide the SQL query inline that will return your entities. This will let you use your own SQL when the generated sql doesn't cut it. For example eager loading a self joining tree using a recursive query.

Now with that said, I think it is easier to initially tweak a custom DAL layer because your are intimate with its construction and can fine tune it; however, a good ORM will provide plenty of hooks that allow you to optimize quite a bit. You just need to spend some time learning it.

I also feel that if you have a performance critical area of code and you can't get your ORM to work within your requirements then for that tiny area of your application you can custom build your own DAL layer. If you're using a decent design pattern such as a Repository created by a factory, then all you need to do is swap out the implementation of your repository

JoshBerke
This makes me think that ORM's are clever enough that they could make scaling easier in some circumstances and have the flexibility to be customised. Should the be the default option when building a site you are intending to have to scale?
danswain
I think they are the default option regardless of scalability because they simplify development and reduce the ammount of code we write and maintain. I could still manually write faster code then an ORM; however, it takes me longer to build and maintain it
JoshBerke
+1  A: 

Hibernate Shards is being ported to NHibernate, which will allow for horizontal scaling.

There are also some very cool hacks like this one to implement sharding.

So the answer is yes, NHibernate can scale, in a persistance-ignorant and fully-transparent way.

Mauricio Scheffer
+1  A: 

It's simply incorrect to say that apps built in an ORM do not scale well. Certainly it has happened before that careless or lazy devs abuse an ORM by writing code that generates horribly inefficient SQL. Building performant apps means understanding something about what all the lovely abstractions actually do under the hood. It does not take much to stay out of this trap however. Using an ORM doesn't mean never opening SQL profiler or NHibernate Profiler.

And regarding the claim that SPs are just a whole lot faster, read this and this. And besides, ORMs (NHibernate, at least) give you pretty easy ways to use SPs if you ever need to.

Tim Scott