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
2009-08-02 16:16:25
+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
2009-08-02 16:19:31
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
2009-08-02 16:26:41
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
2009-08-02 16:28:02
I've started to notice this myself.
Sneakyness
2009-08-03 00:03:21