Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default function values in multi-layer architecture

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.

like image 436
Clay Wardell Avatar asked Nov 03 '22 05:11

Clay Wardell


1 Answers

I would actually do the defaulting at the lowest level that needs a default instead of the highest.

Looking at this from a necessity standpoint -- as far as the resource layer is concerned, 'type' is optional. Same thing goes for your business layer -- there's no logic in there that requires a value for type. It's not until you get down to your data layer that a default value is necessary and has any sort of meaning.

The function in each layer needs to be able to work regardless of what's calling it from "above". So, if your data layer requires some sort of value for 'type' (and there's a default value that makes sense regardless of who is calling it), then put the default value in the data layer.

If you want to be consistent, there's nothing wrong with specifying defaults in higher layers as well. Maybe if some of your business layer functions have logic that requires a value for whatever is getting passed down to the data layer, you could put defaults for all of the functions in that layer. However, I really don't think that's necessary.

The key is that defaults go where they are actually needed.

like image 131
Borys Avatar answered Nov 08 '22 04:11

Borys