Pros and Cons of an ORM

I just came across this blog thanks to a post on Hal Helms Facebook page. Every design pattern we use has some advantages and then some drawbacks, it's just the nature of the beast. We're essentially purchasing the advantages we want by paying with the drawbacks, so the question becomes what is the advantage worth? In the case of a traditional ORM for example, is it worth a certain loss of direct control over our persistence layer (database) in order to theoretically simplify our day-to-day coding tasks.

I think Bill Karwin made some good points in his blog and it's interesting because we tend not to talk about or hear much about the drawbacks of the design patterns we like to use. This is actually just a basic fact of human nature. For example, if you happen to be a big fan of Linux, you're liable to conveniently forget anything about Linux that's challenging or frustrating. And of course the same thing happens with people who are fans of Microsoft. But it's good to remind ourselves from time to time that you can only see polarization from the outside. By this I mean, a fan boy usually doesn't think he's a fan boy. ;)

This blog entry points out the effect of polarization so that you can see the obvious biases between Glenn Block who is an ORM enthusiast and Karwin who is obviously not. In many cases they even disagree about whether something belongs in the pro column or the con column. So looking at the same feature, they have exactly opposite reactions to it. It doesn't get much simpler than this example from Karwin's blog where he gives his opinion of something Block describes as an advantage of ORM systems:

Block: 4. Rich query capability.

Karwin: Absolutely wrong.

I'm always curious to know what other people really think about my software. DataFaucet seems to have been rather well received. But DataFaucet may also be difficult to categorize in particular as an ORM or simply as a Data Access Layer (DAL). Steve Bryant describes his DataMgr tool as a DAL - it's much smaller and much simpler than Reactor, Transfer or DataFaucet. But this sort of thing really depends who you ask, because according to Joe Rinehart, none of the tools billed as ORM for ColdFusion actually fit the definition of ORM. Joe's blog encouraged some additions to DataFaucet that are in source control, but not yet officially released, but, even with added features that make DataFaucet more like traditional ORM tools, does that make it an ORM? Well if you asked Bill Karwin again, who inspired this article, I think he would say no. And here's why I think Karwin would say that DataFaucet is not an ORM:

Block: 2. Huge reduction in code.

Karwin: Depends. When executing simple CRUD operations against a single table, yes. When executing complex queries, most ORM implementations fail spectacularly compared to the simplicity of using SQL queries.

... Block: 4. Rich query capability.

Karwin: Absolutely wrong.

Block: 5. You can navigate object relationships transparently.

Karwin: This is definitely a negative rather than a positive. When you want a result set to include rows from dependent tables, do a JOIN. Doing the "lazy-load" approach, executing additional SQL queries internally when you reference columns of related tables, is usually less efficient. Leaving it up to the ORM internals deprives you of the opportunity to decide which solution is better.

Block: 6. Data loads are completely configurable ...

Karwin: This is not a benefit of an ORM. It is actually easier to achieve this using plain SQL.

My impression is that, although it might be a little slower, Karwin would not have these same objections to DataFaucet, which started it's life as an attempt to abstract the SQL language in a way that Ben Forta declared impossible. The key word was "portability" at the time, but in the process I managed to find ways to not only make querying the database portable, but easier as well. The ability to specify and/or keywords in search queries (think Google) is a prime example of a use case in which standard SQL is a big challenge, but DataFaucet is dead easy. The reason is because for all the ORM features in DataFaucet, it started as a "language". As far as I know, none of the other ORM systems for ColdFusion have approached this particular task.

Although I think Joe Rinehart might have been wrong when he said ColdFusion doesn't have any real ORM systems. If what you want really is a traditional ORM system, I believe there actually is one for ColdFusion. It's a built-in part of the FarCry framework called FourQ. It wasn't included in Steve Bryant's comparison of DAL tools, I think primarily because it's inseparable from FarCry, which if memory serves is an 8MB download (that's compressed). According to their own documentation, the objective of FourQ is to ensure that as a programmer, the word "database" never enters your mind. I'm not sure how effective they've been at achieving that goal, since I don't use it with any regularity. It may work beautifully if that's what you're after. But it does mean that you won't get the kind of querying flexibility that a system designed as a querying language like DataFaucet will give you.

It's not my intention to promote or to bash anyone here (except of course obviously to promote DataFaucet). But I think Karwin made some good points and these are worth considering when choosing between DAL or ORM frameworks.

Comments
James White's Gravatar I would have to say I fall in the Kerwin camp when it comes to ORM. I'd much rather have the control I have now over my SQL code than what ORM offers. I also think using ORM with languages such as Java is more necessary because a Java web developer has to worry about stuff that just happens behind the scenes in ColdFusion. With all the configuration files, the uploading of code to a server, etc. I'm guessing many Java developers do not want to have to worry about writing strong SQL as well. I'm not knocking the Java web development process, but let's be honest, it is involved (no matter how many nice tools are made for it). Especially compared to something like ColdFusion. With that said, I have no problem with the use of ORM or even the way CF 9 has added ORM functionality to the platform. Ultimately whether I like ORM or not, I do think it's worthwhile enough to learn and if necessary use in application development. I am just not totally sold on it yet for CF Development. It just seems to add another level of complexity to code.
# Posted By James White | 8/26/09 6:42 PM
Jake Munson's Gravatar Surprise surprise...Jake's here to comment. ;)

I am very glad you posted this, and I totally agree with your sentiments about fanboyism. I have seen exactly what you've seen many, many times over the years. One good example is beta version of Windows. The beta testers are generally fanboys, who are not critical enough to report bugs they find. The result is that the final product is often torn apart by the "real" testers, who are not fanboys, and do not ignore the bugs. But that's another subject.

I have yet to try an Orm. And listening to people like Hal Helms that have valid reasons why they are a bad idea (in their opinion), I have had even less desire to use one. The problem is that I suspect that people like Hal and Karwin have probably tried an ORM before, and they have experience in both worlds. I have not tried one, so I can only lamely say that I like SQL better. :\
# Posted By Jake Munson | 8/26/09 10:27 PM
Ben Nadel's Gravatar I'm still on the fence as far as ORM goes. It seems very cool, but at the same time, I have really only tried the "Hello World" type examples, although they do include inheritance, distributed persistence, and various relationships.... but, I haven't done anything yet that is complex. I wonder if it will hold up in those situations. I'm still learning, so I haven't made up my mind yet.
# Posted By Ben Nadel | 9/1/09 4:31 PM
ike's Gravatar Thanks Ben... I think the comment of "haven't made up my mind yet" is one of those really-important, but under-the-radar comments. I think we have a tendency as programmers, just because we're regular people like everyone else, to rely a lot on habit rather than on a situation at hand. So we often make comments about liking or not liking solution x in absence of the context -- when the reality is that the context is what makes that solution useful or lame. If all your applications are tiny, then loads of caching features for example would be pretty lame. But right now I'm working on a massive caching framework project called CacheBox that, in the right context, when you have an application that you think might grow or that's becoming a challenge in terms of resources, then some kind of advanced caching framework makes absolute sense. ;) I was reading an article that I think Mark Mandel wrote recently for dzone in which he said that opinions of ColdSpring tend to be very hot or cold, people either really love it or they just see it as busy work -- and he said he thinks the difference is whether you've worked on an application where ColdSpring was actually really useful. If you hadn't worked on one of those apps, then you wouldn't have the context that lets you appreciate the solution. Or basically that you have to feel the pain before the aspirin does anything for you. ;)
# Posted By ike | 9/1/09 7:10 PM
Jake Munson's Gravatar I think another thing to consider is whether the solution replaces anything or not. For example, ORM replaces cfquery, which most of us are happily using without complaint. So it's hard to convince someone to use a different solution when they're happy with what they've got.

In contrast, your new caching framework doesn't really replace anything (if I'm not mistaken). Well, I realize that CF has some built-in caching capabilities, but they don't cover everything. So I'd be much more inclined to use CachBox because it fills a hole.
# Posted By Jake Munson | 9/2/09 11:17 AM
ike's Gravatar Thanks Jake. Sorry I didn't reply to your earlier comment. I think you're right that replacing something people are already using is a harder sell than something new. One of the things I really like about DataFaucet is that it considerably reduces my code volume compared even to using no DAL framework at all and using just cfquery. The documentation contains some information about how I ported Ray's Galleon Forums to DF and in addition to carving out over 600 lines of code, I added features to it at the same time. Now 600 lines of code may not seem like much -- I have a couple of CFCs that are as long, but Galleon isn't a large application either. Over the course of a large application, that adds up. Getting back to your point, it's not immediately obvious when I say "ORM" that you're going to get that benefit over just using cfquery. And even if you do switch, you are trading those lines of code for a system that will inevitably be a little slower just because there is overhead in any system. So I think yes, a system that replaces something else definitely requires an explanation as to why the new solution is an improvement over the original. But even with caching, I've worked on some hand-rolled caching systems in the past and in conversations on the cf-talk list I've seen people say "why would you do that when there's query-caching built-in?" So even Cachebox isn't immune to competition. ;)
# Posted By ike | 9/3/09 12:05 AM
Jake Munson's Gravatar Yeah, I don't know everything you've got planned for CacheBox, so I don't know how much it "competes" with built-in CF stuff. But I do know there are places in most applications where CF doesn't provide caching, and using a framework would be very helpful.

I still want to try DF some day, as I think you've made a compelling tool. From our past conversations, it seems that you have paid more attention to complex DB operations than I've seen with other ORM tools. But the trouble is, I don't really have a need for it at the moment. At work, my teammates would be very much opposed to using an ORM, and I am not working on any hobby projects at home (too much other stuff going on). But I do hope to use it some day. :)
# Posted By Jake Munson | 9/3/09 10:13 AM
gary f's Gravatar I think the usefulness of an ORM to some extent depends on the complexity of the data. A simple store with a few objects, ORM is probably the way to go. A Clinical Trials system, highly normalized with per sponsor override tables for each 'real' table in your system. The number of joins becomes complicated quickly. A decorator for your decorator tables, oh boy could that get complicated.

Its one of those depends. Do you have a huge object graph? Are you using small hardware, or can your server hold the object graph that the ORM will load?

(Do you want to cache under your control or the ORM?)

How closely do you OBJECTS match to your TABLE. This may be a preverse statement but I believe the value of the ORM goes up when the need for the MAPPING goes down ...

just my opinion...
# Posted By gary f | 9/4/09 1:19 PM
tony petruzzi's Gravatar while i agree with some previous statements that ORMs don't always fit the bill when it comes to doing complex queries against a database, i think that people look at ORMs as simply a CRUD tool and forget that they provide so much more. the main benefit of using an ORM to me is that it encapsulates the business logic of my application in one place. sure you can do the same thing with a mess of components, but why when an ORM has all the hooks in place for you.
# Posted By tony petruzzi | 9/11/09 11:48 PM
ike's Gravatar Yeah, I like one-stop-shopping...

although you do have to be a bit careful that you don't over-rely on a particular tool or a particular technique (what they call the "golden hammer"). Anything can become a golden hammer I think if you let it... I think when you've got a list page/view a query (or a single object wrapped around one) makes a heck of a lot more sense than trying to get an array of objects. And yet, some people still try to get that array of N objects even though at that point OO has become the golden hammer.

I was just reading Marc Funero's article on his blog the other day about how he drove himself nuts trying to shoehorn everything he did into an unachievable standard of OO purity. http://www.advantexllc.com/blog/post.cfm/how-oo-al... This is not to say that everyone's going to do this, but human nature being what it is, it's easy to get pulled down that kind of a path to one extent or another. I've done it a few times. I don't think I've ever done it to the extent Marc described, but I could relate to some of the things he mentioned.
# Posted By ike | 9/12/09 12:50 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.5.006. | Protected by Akismet | Blog with WordPress