ORM, schmorm
Here is a blog post by someone who dislikes ORMs. I guess you know what I have in store now … A HORRIBLE RANT!
Actually not. In some ways I agree with him, and in some ways I disagree with him. You could call it a more nuanced view, perhaps? I don’t agree with much of what he writes, though. Let’s get that out of the way first. I agree with him that ORMs may introduce overhead and that it certainly is possible to write clean and efficient code that doesn’t use an ORM if you know how the database works.
Ok, so my perhaps my view isn’t that balanced, because he writes things about preferring procedural code and using triggers. What is this, the 80s? Triggers may be efficient, but they tie the application too much to the database implementation and creates problems if you want to change the database implementation (and I live in a world where that happens). Also, it makes it harder to debug and develop, with more steps that are needed in order do deploy code that has changed.
And as for the rest… Well, I like objects, he doesn’t - not much more to say about that. But as your code grows you will basically end up writing an ORM yourself, although perhaps tied to that particular application. And then you start making changes and you may find out it’s too rigid, because you can’t reuse your code. Or you start a new project and have to create yet another application-specific “ORM”. After a while you get tired, and realize that bright people have spent a lot of time on writing ORMs, and they even have caching now! Yes, it’s true… I know, it’s amazing (that’s sarcasm, in case you didn’t notice).
Some ORMs have quite nice caching, and you can select numerous fetching strategies for retrieving objects, which can limit amount of SQL statements executed. Speaking of SQL statements, his comment about hearing of “popular CMS systems and web frameworks that will make dozens of database calls to refresh a single page” is probably true, but is it due to ORMs or lazy/incompetent programmers? You’d be amazed (or perhaps you wouldn’t) about the amount of crappy code people are guilty of writing (myself included, I’m not embarrassed about that).
In the end though, ORMs are about convenience and ease of programming substantial applications, then performance. I don’t think anyone is trying to hide that fact. For me, and many others, the benefit of using a more abstract model is more efficient (brain-wise) than using the old school style, as he recommends. I much prefer writing something like:
@Entity
public class Person {
Integer id = null;
String name = null;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() { return id; }
pubic void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
… and then instantiating that class and storing/retrieving objects, than starting to write a database schema to define my model.
As for the performance problems he rants about, there are solutions to improve that. Perhaps he should get acquainted with a modern ORM.
Inspired by