views:

104

answers:

2

Are data access objects an exception to the rule 'Tell, don't ask'? Eg get last 10 posts from a table data gateway?

+6  A: 

One summary of Tell Don't Ask says:

"you should endeavor to tell objects what you want them to do; do not ask them questions about their state, make a decision, and then tell them what to do."

In the case of asking a data object for information I don't think we violate that principle.

Consider displaying a report or UI with "Person" information. I don't see much alternative to saying "What's your name?" Note that we don't follow up with any "tell them what to do".

djna
+3  A: 

Technically aren't you telling them to tell you something about themselves when you ask them about themselves?

OOP gives me a headache sometimes, I'm never sure when to stop thinking and start writing, or to stop writing and start thinking :X

Sneakyness
In this case, consider whether your asking question would require the object itself to change. The thing we're being advised against is breaking the encapsulation of that internal state and it's relationships. For exmaple suppose an Employee class has salary, units sold this month and commision. We dont query the salary and the sales and set the commision - we expect the Employee to look after that and answer questions such as "what's the commision".
djna
Oh, and when to start and stop thinking ... I reckon that tends to come with experience. Often insight comes when you try to maintain code you wrote a while back - aha! if only I had thought better then!
djna
I've started to notice this myself.
Sneakyness