Aren't Information Expert / Tell Don't Ask at odds with Single Responsibility Principle?
Posted
by moffdub
on Stack Overflow
See other posts from Stack Overflow
or by moffdub
Published on 2008-10-04T00:48:40Z
Indexed on
2010/06/02
6:33 UTC
Read the original article
Hit count: 404
It is probably just me, which is why I'm asking the question. Information Expert, Tell Don't Ask, and SRP are often mentioned together as best practices. But I think they are at odds. Here is what I'm talking about:
Code that favors SRP but violates Tell Don't Ask, Info Expert:
Customer bob = ...;
// TransferObjectFactory has to use Customer's accessors to do its work,
// violates Tell Don't Ask
CustomerDTO dto = TransferObjectFactory.createFrom(bob);
Code that favors Tell Don't Ask / Info Expert but violates SRP:
Customer bob = ...;
// Now Customer is doing more than just representing the domain concept of Customer,
// violates SRP
CustomerDTO dto = bob.toDTO();
If they are indeed at odds, that's a vindication of my OCD. Otherwise, please fill me in on how these practices can co-exist peacefully. Thank you.
Edit: someone wants a definition of the terms -
Information Expert: objects that have the data needed for the operation should host the operation
Tell Don't Ask: don't ask objects for data in order to do work; tell the objects to do the work
Single Responsibility Principle: each object should have a narrowly defined responsibility
© Stack Overflow or respective owner