Default Function Values ​​in a Layered Architecture

Satisfying the best way to set defaults in a tiered application structure. In particular, if a specific workflow requires a nested set of function calls, is it set to the default for all functions, or is it just a top-level function and passed down? Or is it some other template?

For example, consider a web application with three layers:

  • The resource level processes HTTP requests and responses, receives HTTP parameters from the client
  • The business layer implements the business logic necessary to determine the necessary information.
  • The data layer accesses the database and returns the requested data.

Suppose that the client wants to get one object - for this example, you can say its Place object. Object objects have type - city, state, city, county, etc.

A 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 may 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 

If two functions below the resource level in this stack also default to None? Part of me thinks this will violate DRY. Another part of me thinks that the most predictable and modular way to do this would be to use None for every function in the stack by default.

+4
source share
1 answer

I would default at the lowest level, which requires a default instead of the highest.

Considering this from the point of view of necessity, as far as the resource layer is concerned, the β€œtype” is optional. The same goes for your business layer β€” there is no logic that requires a value for the type. This is not until you go to your data layer that the default value is necessary and makes any sense.

The function in each layer should work regardless of what calls it from the "above". Thus, if your data level requires some kind of value for "type" (and there is a default value that makes sense no matter who calls it), then put the default value in the data layer.

If you want to be consistent, there is nothing wrong with specifying default values ​​at higher levels. Perhaps if some of the functions in your business layer have logic that requires a value for what is passed to the data layer, you can set default values ​​for all functions in this layer. However, I really do not think this is necessary.

The key is that the default values ​​go where they are really needed.

+1
source

All Articles