Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python服务层_Python_Service Layer - Fatal编程技术网

Python服务层

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

我正在使用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'))
    ...
获取组添加组方法属于哪里?部门服务还是团体服务

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