gpt4 book ai didi

Python服务层

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:05 25 4
gpt4 key购买 nike

我正在使用 Python 2.7.2 和 SQLAlchemy 0.9.3。一个部门有很多组。

class Department(Base):
id = Column(Integer, primary_key=True)
groups = relationship("Group", backref="department")
...

class Group(Base):
id = Column(Integer, primary_key=True)
department_id = Column(Integer, ForeignKey('departments.id'))
...

get_groupsadd_group 方法属于哪里?在 DepartmentService 还是 GroupService 中?

class DepartmentService(object):
def get_groups(self, dept_id):
...
def add_group(self, dept_id, group):
...

class GroupService(object):
def get_groups(self, dept_id):
...

def add_group(self, dept_id, group):
...

或者我从 DepartmentService 调用 GroupService 吗?

最佳答案

本回答纯属个人观点。可能有一个我不知道的更好的技术原因。

两者都怎么样? 伟大 backref功能将为您保持数据库同步:

class DepartmentService(object):
def get_groups_by_department(self, dept_id):
...
def add_group_to_department(self, dept_id, group):
...

class GroupService(object):
def get_department(self, dept_id):
...

def add_to_department(self, dept_id, group):
...

如果我必须选择一个,我会把它放在 GroupService 中,主要是因为函数的写法比较自然,通常意味着更适合 ;-)

关于Python服务层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22970329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com