Are there any patterns/solutions to solve the problem of having an API call which in one scenario may need to return a full object graph (say a manager object and all it’s staff relations) and in another may just return a single manager object?
The ideas we’re currently passing around are:
- Many methods
In the example above you’d have one method calledget_manager()and another calledget_manager_deep(). - Many objects
Similar to 1. you have two methods, one returns amanagerand one returns amanager_with_staff - Data-driven API
Here the method accepts some kind of query (maybe XML) which defines exactly what kind of object you want back, and the method returns perhaps a dynamic object matching the query.
NB. These examples are very simplified, in reality the methods may need to specify where parts of the data comes from – ActiveDirectory/DB.
Anyone ever come across problems like this?
EDIT – Main driving force of the design is to have a clean and simple (but effective) API and object model. So some of the above examples result in potentially large (and possibly confusing) object graphs. Having too many (hopefully unnecessary) methods/classes can be a maintenance problem.
You should try creating one according to your needs.. you just need two concepts..
1. Method Chaining
2. Recursion
A really good article of what Method Chaining is at Wikipedia
http://en.wikipedia.org/wiki/Method_chaining
Regards.