有人可以帮助我进行代码体系结构/设计吗? 我需要处理一些包含将近30个参数的大型表单(它是来自前端的json):
我需要处理这些数据:
所以代码看起来像这样(伪代码,实际上我使用的是python / django)
params = serializer.data # (now django-rest_framework is used, but it can be just reguest.GET - it does not matter I think)
user = register_user(
email=params['email']
first_name=params['last_name']
last_name=
city=
......
~ 10 params
)
NotificationService.send(
first_name=params['last_name']
last_name=
time=
aggree_1=
aggree_2=
amount=
bank_code=
person_type
~25 params
)
srv = PaymnetService(
user=user
fisrt_name=params['last_name']
last_name=
country=
city=
paymnet_method=
amount=
...
~ 18
)
srv.run()
create_referal(user, params['ref_code'])
“ PaymnetService”(和其他)看起来像
class PaymnetService():
def __init__(self, ~20 params):
self.user=user
self.paymnet_method = paymnet_method
self.issuer = issuer
self.amount = amount
self.bank_code = bank_code
self.last_name = last_name # payment privoder requires it
self.client_ip = client_ip # payment privoder requires it
self.country_name = country_name # payment privoder requires it
....
self.param_2 = param_x
def run(self):
is_valid = self.verify()
if valid:
geteway_response = self.call_paymnet_geteway(self.param_x ....)
self.create_local_order(self.param_x, geteway_response['payment_id'] ...)
self.send_other_notification(user.email, geteway_response['id'], geteway_response['result'] ....)
def create_local_order(self):
..............
问题是我有很多参数,并且生成处理程序(create_user,NotificationService,PaymentService ...)需要很多这些参数,但不是全部! 我的大部分代码都由诸如“ param_x = params ['xxx']”之类的行组成(将参数粘贴到其他(更具体的处理程序)。 而且我不希望只是通过整个“PARAMS”对象(serializer.data在我的情况),因为并不好看(“隐性”) 所以问题是:是否有任何实用的建议如何更好地组织我的代码?
我当时正在考虑使用namedtuple,数据类作为不同(或仅一个)参数组的容器。但是不确定这是否是个好主意,因为很难对传入的参数进行分组(在使用多个组的情况下):例如,有“ country”和“ country_origin”参数,但是某些处理程序仅需要其中一个,而某些则需要两者