Re: Mixing OO and DB

From: Robert Martin <>
Date: Sun, 2 Mar 2008 13:38:37 -0600
Message-ID: <2008030213383743658-unclebob_at_objectmentorcom>

On 2008-02-28 19:05:51 -0600, topmind <> said:

> Patrick May wrote:

>> frebe <> writes:
>>>> We still have it, and we still use it.  But we also hide it from
>>>> the bulk of the application.
>>> What is the benefit with hiding SQL from the bulk of the
>>> application?
>> For one thing, it decouples the application code and the database
>> schema.

> No it doesn't because the mapping has to be coded *somewhere* anyhow.

The mapping is coded in source modules, of course. However, the main application has no source code dependencies on those modules. Therefore they are easy to replace with dummies or alternate implementations.

>> For another, as noted by Mr. Martin, it allows the creation
>> of a domain specific language that better reflects the concepts in the
>> problem and solution domains.

> But it is not "upping" the abstraction enough to justify all the
> method proliferation.

Sure it is. find_eligible_employees is at a higher level of abstraction than select * from ... and ... where ... < ... or ... ... ... ;

The abstraction level is also "upped" because the method is decoupled from it's implementation (meaning that the source code that calls the method has no source code dependency on the implementation).

> It's almost like:
> function addTwoNumbers(a, b) {
> return (a + b);
> }

No, it's more like:

Money netPay(Money earnings, Money deductions) {   return earnings - deductions;

The calculation has a *meaning* or intent; and the method provides the name of the intent.

>>> If you have a lot of find_by_xxx methods, swapping would still be
>>> quite difficult.
>> That is true.  In my experience there isn't usually a
>> proliferation of such methods because traversal is done by object
>> reference rather than by some subset of object state.

> In other words, your app reinvents a (navigational) database in the
> app. You can query the DB less because your app becomes a (half-ass)
> DB.

Not quite. The app because an *application specific* DB. That's not half-assed at all.

> OOP 101:
> 1. RDBMS are Satan.

That's actually a very foolish statement that flies in the face of an immense body of data. There are very few OO applications out there that don't make heavy use of relational databases.

Robert C. Martin (Uncle Bob)  | email:
Object Mentor Inc.            | blog:
The Agile Transition Experts  | web:
800-338-6716                  |
Received on Sun Mar 02 2008 - 20:38:37 CET

Original text of this message