Wondering the best way to set defaults in a multi-layer application structure. Specifically, if a certain work flow requires a nested set of function calls, is the default specified on all the functions, or just on the top level function and passed down? Or is it some other pattern entirely?
For example, consider a web app with 3 layers:
- The resource layer handles HTTP requests and responses, gets HTTP parameters from the client
- The business layer performs the business logic required to determine what information is needed
- The data layer accesses the database(s) and returns the requested data.
Lets say the client wants to get a single object -- for this example, lets say its a Place
object. Place objects have a type
-- city, state, town, county, etc.
The resource function might look like this (using Django HTTP objects):
def resource_get_place(request, type=None):
""" Gets a place of the specified type, or a place of any type if
type is None """
place = business_get_place(type=type)
return HttpResponse(request, {
"place" : place
}
Then the other two might be:
def business_get_place(type):
# Trivial in this case, used for consistency
return data_get_place(type)
def data_get_place(type):
# Get a place of specified type from the cache, or db,
# or wherever else. If type is None, get place of any type.
return place
Should the two functions 'below' the resource layer in this stack also default type to None? Part of me thinks doing so would violate DRY. Another part of me thinks that the most predictable and modular way of doing it would be for every function in the stack to default type 'sensibly' to None.