Python服务层
我正在使用Python 2.7.2和SQLAlchemy 0.9.3。 一个部门有许多小组Python服务层,python,service-layer,Python,Service Layer,我正在使用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) depar
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'))
...
获取组和添加组方法属于哪里?部门服务还是团体服务
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
,主要是因为函数的措辞更自然,这通常意味着更适合;-)我得选一个。现在,我将放入GroupService并将其注入DepartmentService。是的,backref是惊人的:D