Why should you use an ORM? [closed]

The most important reason to use an ORM is so that you can have a rich, object oriented business model and still be able to store it and write effective queries quickly against a relational database. From my viewpoint, I don’t see any real advantages that a good ORM gives you when compared with other generated DAL’s other than the advanced types of queries you can write.

One type of query I am thinking of is a polymorphic query. A simple ORM query might select all shapes in your database. You get a collection of shapes back. But each instance is a square, circle or rectangle according to its discriminator.

Another type of query would be one that eagerly fetches an object and one or more related objects or collections in a single database call. e.g. Each shape object is returned with its vertex and side collections populated.

I’m sorry to disagree with so many others here, but I don’t think that code generation is a good enough reason by itself to go with an ORM. You can write or find many good DAL templates for code generators that do not have the conceptual or performance overhead that ORM’s do.

Or, if you think that you don’t need to know how to write good SQL to use an ORM, again, I disagree. It might be true that from the perspective of writing single queries, relying on an ORM is easier. But, with ORM’s it is far too easy to create poor performing routines when developers don’t understand how their queries work with the ORM and the SQL they translate into.

Having a data layer that works against multiple databases can be a benefit. It’s not one that I have had to rely on that often though.

In the end, I have to reiterate that in my experience, if you are not using the more advanced query features of your ORM, there are other options that solve the remaining problems with less learning and fewer CPU cycles.

Oh yeah, some developers do find working with ORM’s to be fun so ORM’s are also good from the keep-your-developers-happy perspective. =)

Leave a Comment