想知道在多层应用程序结构中设置默认值的最佳方法。具体来说,如果某个工作流需要嵌套的一组函数调用,则默认是在上指定的所有函数,还是只在顶层函数中传递?还是完全是一些其他模式?多层体系结构中的默认功能值
例如,考虑一个Web应用程序与3层:
- 资源层处理HTTP请求和响应,从客户端
- 获得HTTP参数业务层执行,以确定所需的业务逻辑需要信息
- 数据层访问数据库并返回请求的数据。
可以说客户端想要获得一个对象 - 对于这个例子,可以说它的一个Place
对象。地方对象有一个type
- 省,市,镇,县等
的资源功能可能是这样的(使用Django HTTP对象):
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
}
然后其他两个可能:
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
这两个函数'下面'这个堆栈中的资源层是否也默认类型为None?我的一部分认为这样做会违反干。我的另一部分认为,这样做的最可预测的和模块化的方式是在堆栈中的每个函数默认键入'合理'到None。