This is a very interesting thread. I've been doing my own ORM soul searching recently, too. At this point, I'm an ORM skeptic, and becoming more so over time. In fact, I kind of hate the ORMs I've used.
For this reason I paid particular attention to the things people have said are good about ORMs. To all of you, thanks for talking about concrete reasons you like an ORM. Attempting to do that is how reasonable people learn from each other.
I'd like to distinguish between two meanings of the word ORM so that when I start making some arguments later on, we don't get hung up on definitions.
I'm going to define "type 1" ORMs as "any layer that allows a more abstract layer to speak only in terms of native language constructs." This definition is a literal interpretation of the acronym: Object Relational Mapping. In this definition, ORM stands in opposition to "directly manipulating hashes/dicts/maps of strings:scalar values at all layers of abstraction." This is the definition you'll find on Wikipedia, by the way.
As far as this goes, I agree that speaking in terms of native language constructs is going to be useful to any program that is complicated enough to be interesting. To provide a motivated example, if any layer of abstraction at all is to speak in terms of a date/time object instead of the string "2012-08-19 14:01:32", then there will have to be some part of the application that performs that translation in each direction. If you factor that translation into its own layer so that you can share code, and call that part an ORM, then you are literally correct, and what's more, that layer is doing something useful for you. If you go on to organize your code so that all data access goes through this inflation layer, then you've got a pretty good thing going on, in my opinion.
In Type 1 ORMs, you define your own "bespoke" retrieval and storage methods, that know how to get exactly the information that you want out of the database. If you frequently need to get the 7 most recent Passenger widgets and their related FlightInfos, then you write a retrieval method that knows how to query for the 7 most recent Passenger widgets and their related FlightInfos and then inflate them. The rest of your app just asks for the 7 most recent Passenger widgets and FlightInfos in one gulp and never thinks about how it got them. This is as abstract as you can wish.
Things that you get from just type 1:
ShuggyUK":ox718a5u said:
tinyMan":ox718a5u said:
"inflating" the related entities into a single Entity-Map that is far easier to work with in code and more importantly, easier for me to mentally map out.
But most ORMs go one step further. They are a place to inflate and deflate objects. But they do something else as well: They replace SQL with another API.
I call "type 2" ORMs layers that do "inflation/deflation" plus provide a "query building API". Sure, in the better type 2 ORMs, you can "drop into" SQL whenever you need to, but the preferred method of getting your data is by calling methods provided by this SQL-replacement API. If you use the API, then it can do a lot more of the work of inflating your objects for you. In type 2 ORMs, you need you to write out all of your table relationships ("Foo has many Bars. Bars have one Foo.") In return for writing this relationship out, the API promise to build those messy join queries and tease out the objects that result from that messy data.
Basically, type 2 APIs have these big query building engines under the hood that take some of the work away from you, particularly in constructing joins among related tables that have their relationships mapped out in code.
Type 2 ORMs define an API that lets you construct, on the fly, a query that's going to return the 7 most recent Passengers and all their FlightInfos. But constructing your queries willy nilly throughout the application using this extensible API is a nasty abstraction leak: Your ways of getting data are scatted about the app, in situ where that data is needed. You can imagine a world in which a second developer, not knowing it'd already been done, or unable to conveniently reuse the first's work, independent builds up a query for the "7 most recent passengers." Both of these hypothetical devs will be optimizing and thinking about how data is stored all over the app, instead of just in the data layer, where these things are supposed to live. And for what? For WHAT?
I've gleaned all the things that people seem to like about type 2 ORMs. They amount to petty conveniences that can be dispensed with in two hundred lines of helper functions. Why does the thing that makes these essentially convenience-related code reductions possible have to be the same thing that knows how to inflate and deflate objects?
Solomonoff's Secret":ox718a5u said:
Hibernate's Criteria API is a godsend for filtering rows by many dynamically enabled restrictions, or applying the same filtering conditions to a dynamically chosen table - it's a hell of a lot more convenient than concatenating snippets of SQL for the applicable filters.
Solomonoff's Secret":ox718a5u said:
Hibernate's HQL is more convenient for joins across entity relationships than SQL
.
herko":ox718a5u said:
Specify joins as sub objects contained within objects
tinyMan":ox718a5u said:
Being able to ask the ORM-thingie for a Customer object and getting back a CLR object ("entity") that then has (none, some, all) of the related entities logically attached.
It seems like to get all of these useful features, it is not necessary to replace SQL with something else. You can do the same thing yourself by having an inflation/deflation engine and a helper module that takes the pain out hand crafting the common cases that ORMs are good at handling. Instead of indirectly controlling a SQL generation engine, why not just directly control it? Why have Rube-Goldberg like indirection layers between you and the query builder? This is not a rhetorical question. I would really like to know if there is something that I'm missing here - please fill me in.
If it's not clear already, I think we should take the inflation/deflation stuff and keep it. It's good. Then, instead of living in a world in which we default to a different API and switch to SQL when stuff gets hard, we should invert that: Use SQL, and call on helper functions to make writing that SQL more convenient whenever we find ourselves writing boilerplate. We get one really sweet bonus from doing it this way: we can use those same helper routines when we're doing the hard stuff. We never have to give up all of our candy in order to do the hard stuff. In engineering I believe you will find that making hard problems easier is more important than making easy problems easier.
In order to directly praise a type 2 ORM, you have to be prepared to argue that Foo ORM's API for data access or insertion is superior to SQL in most cases, and in ways that can't be ameliorated with small and easy to understand helper routines - a very difficult claim to make, when Foo ORM is ultimately limited by SQL in any case. No matter how hard or easy you think it is to balance a canoe on your nose, it's harder to balance a stick on your nose and balance a canoe on top of the stick.
SituationSoap gamely makes the attempt to argue that stick+canoe is better than canoe, however:
SituationSoap":ox718a5u said:
I see the ORM/SQL debate as generally similar to the Assembly/C divide from years gone past...I wouldn't be surprised if, twenty years from now, the idea of programmers writing SQL on a regular basis is considered quaint. The tools aren't there yet, but we're moving in that direction, and I say we're better for it.
But the Assembly/C analogy is horribly invalid - about 180 degress off of correct. SQL purely declarative! All other APIs for describing a relational data set can only approach its level of abstraction asymptotically.
herko kind of also argues this, in a sense of the old "I can write better assembly in C than I can in assembly" argument.
herko":ox718a5u said:
The SQL it writes is quite good, too
But it seems pretty weak to me. If you can specify your ORM query in such a way that it can write efficient SQL, then you can write efficient SQL. In fact, it's easier to write good SQL, and not just because the tooling is more mature, but because SQL is specifically designed to be an efficient expression of a data set.
herko":ox718a5u said:
SQL-platform independence
And then he trots out this old fallacy. I feel like I've had to defend against this fallacy many times in the last few weeks. Listen. You only get platform independence if you voluntarily give up vendor-specific SQL extensions, e.g. by never dropping down into SQL or by voluntarily restricting yourself to the lowest common denominator of SQL whenever you do drop down. But you'd get that same platform-independence if you restrained yourself to ANSI SQL when you were hand-crafting, too. The only reason you wouldn't restrain yourself is because vendor SQL turns out to be pretty useful, and you'd rather have a useful feature in hand now than an abstract and tenuous future advantage.
Now, I don't understand what these things are, so I can't classify them:
ShuggyUK":ox718a5u said:
automatic conversion to arguments rather than string concatenation
herko":ox718a5u said:
The declarative extension
Could you guys explain what these are?