3 ms·
In this discussion, everyone seem to broadly assume that it's an all-or-nothing affair. More precisely, it's a cost-benefit decision. If most queries you will
by lloeki 15y ago
In this discussion, everyone seem to broadly assume that it's an all-or-nothing affair.
More precisely, it's a cost-benefit decision. If most queries you will make are hampered by the ORM then by all means, don't use one. But if like in many (most?) situations, an ORM greatly abstracts and eases design and development for 90%+ of your operations and you have like 10% queries to be either tuned or handwritten (even if it has to be handwritten against multiple database types to preserve portability), then an ORM is a net benefit.
Compare it to inline asm in C, or C modules in Python: the higher level stuff makes it efficient to work with top level concepts 90% of the time, but sometimes you have to go down to be actually efficient, or even simply be able to do something, even if that means losing some form of independence (which would then mandate writing the same function for a different platform if you want to preserve portability). Not only it is not an anti-pattern, by no means does it mean either that the abstraction is fundamentally flawed.
The very idea that "going around" an ORM is somehow proving that ORMs are flawed is simply wrong. There are problems that ORMs are built to solve, and there are problems they can't ever solve. "Going around" is part of the deal because it's not a "work around", it's a "work together".
This is very visible in the article, especially the moment the author states that "I claim that the abstraction of ORM breaks down not for 20% of projects, but close to 100% of them". Indeed this is the case, but for close to 100% of those close-to-100%-projects where it "fails", the ORM is helpful for managing 90% of data access implementation. The 10% remainder may need to be implemented at a lower level, but wouldn't it be silly to spend a lot of time on those 90% of code that would get used 10% of the time? This is what ORMs are about, and saves a lot of time to develop the 10% of code that is critical both in usage volume and in performance. Of course such ratios are highly project dependent, and this is what warrants a thoughtful analysis to select the right tool for each task, of which there can be multiple in a single project, or even _object_. ORM, just as NoSQL, is simply not the end-all be-all solution, yet that does not make it a very valid pattern any less.
(edit: cosmetic/typo)
- oelewapperke 15y agoWhat I'd like to see some discussion for is what consequences ORM has that SQL seems to manage to avoid. What I mean is that any ORM, even a theoretically perfect one, requires that you write an entire datamodel in your application that manages to remain consistent under modifications. What a chore ! Then you get to write a battery of tests (and I mean a battery, dear God, this is many tests) What a chore ! And I don't mean this in the sense that I want to get out from under it, but you have to admit, making a datamodel remain consistent in-memory ... it's not hard ... it's not challenging ... but it's very hard to get that last bug out of it, and SQL ... well they've done this for you. So why would you bother doing this ? And before you say it, as anyone who wrote a few webapps knows : you DON'T need a datamodel in your application to create very complex webapps. In fact one of the big reasons for the (unfortunate) popularity of PHP is that you don't. None of the ORM frameworks solves the data consistency problem half as elegant as SQL does. So why bother ? There's one good answer to that question : because it doesn't scale (although 99% of sql does scale, just large reporting queries don't. Of course, these don't scale no matter what system you use, it's just that sql makes it very easy to build this). Also : take a look at how SAP products work. They solve the problem of big reporting queries by having massive amounts of triggers generate the reports for them. This scales, is easily distributed, and it's amazing what it can do. Of course, what it can't do efficiently is give you new reports based on old data.