Pragmatic Prose
Sunday, December 16, 2007
ORM's Are Not The Answer
Or ORM's Are Not the Whole Answer
OR Some ORM's Are A Better Answer
One point I feel I need to make. So many people seem to think that at some point an ORM will appear that will just magically work. It will map all your entities to objects and "just work."
So an endless parade of ORM's continue to lurch forth from the hinterlands.
I'm amazed that people like Bruce
Eckel think that an ORM powerful enough
will just solve the problem.
The thinking is usually something like "we can shield the user from that ugly, mean SQL with some nice procedural and markup tricks."
I've come to believe that anytime you try to hide/wrap/abstract a powerful technology/language (SQL, JavaScript, HTML, etc) you end up with at best a leaky abstraction and at worst a complete mess.
SQL is concise and powerful. It doesn't need to be hidden. It's not that hard to understand. It's at least easier to understand than most of the ORM frameworks. It's quite ridiculous what kind of code acrobatics you must perform to add parameters to a query.
One of the biggest problems with an ORM is predicting what kind of code they will generate/execute. The more considerate frameworks include a logging function of some sort. This is nice but when I go to look at some code and I find a bunch of ORM garbage code, I find myself wishing for the SQL.
I used to hate embedded SQL, then I realized there really is nothing wrong with it, and it fact it's preferable as I found out what a web page is doing all in one file, without analyzing a (now unsupported) ORM or trying to find a stored procedure in the database.
The stored procedure approach is not that bad, I just find myself shying away from them as code re-use is a myth. Yes, now I've become a true OOP Heretic. Code re-use, especially between projects, is a rarity. And this is the major problem with ORM's, they tend to emphasize the one object for every situation paradigm.
If I have an employee table, naturally I will have an employee object, if I'm an OOP ORM
Myrmidon
. So I try to use this Employee object for my employee edit screen, my employee absence report, my employee salary report, my company org chart, etc, etc. However, I soon find my nice clean ERD and UML diagrams become corrupted by the business needs. We have to join in extra tables. Pretty soon we are writing all manner of custom logic in our programming language of choice. We have to use a little somthing like join_to(tables.employee_benefits).get_by(table.columns.user_name='a%').limit(10).sort_by(table.columns.user_name).
We are wasting resources by bringing back unneeded columns. Because the One True Employee contains fields for every situation, we populate the whole object for every request. We can resort to lazy loading and other performance optimizations for expensive entities.
The
Better
Solution
The answer is that there is no answer. However, I think the frameworks like
CoolStorage.NET
and the db library in
Web.Py
have a nice ability to load data from queries into objects. This takes away about 80 percent of the pain of Database/Object interaction and let's us use queries specific to the Use Case at hand.
I think the best we can do is to use the best tool for the job, in the case of talking to databases that tool is SQL. We should use SQL and take advantage of automatic mapping.
Learning SQL one time is much better than learning a new ORM every year. I feel the same for HTML, JavaScript, CSS and the rest of fundamental technologies of our trade.
C#
|
Design
|
Languages
|
SQL
Sunday, December 16, 2007 3:54:42 PM (Central Standard Time, UTC-06:00)
Comments [0]
Name
E-mail
Home page
Remember Me
Comment (HTML not allowed)
Enter the code shown (prevents robots):
© Copyright 2008 Chris Weber
Theme design by
Bryan Bell
newtelligence dasBlog 1.9.6264.0
| Page rendered at Friday, July 04, 2008 6:28:55 AM (Central Standard Time, UTC-06:00)
On this page....
<
July 2008
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
Search
Navigation
Home
newtelligence AG
dasBlog
SourceForge
Scott Hanselman
Omar Shahine
Categories
ASP.NET
biz
Books
C#
Design
firefox
General
Greasemonkey
hacks
Hardware
Implementation
Java
javascript
Languages
Laszlo
Money
organization
personal
PowerShell
Productivity
Prometheus
Python
Ruby
Ruby on Rails
SOA
SocioEcon
SQL
Tools
Web
Windows
Blogroll
Healthy Reader
Pop Therapy
Slippery Brick
The Art of Ware
Wealthy Reader
What's your 20
Sign In