Python服务层

时间:2014-04-09 17:54:07

标签: 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)
    department_id = Column(Integer, ForeignKey('departments.id'))
    ...

get_groups add_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?

1 个答案:

答案 0 :(得分:1)

这个答案纯属我个人意见。可能有一个我不了解的更好的技术原因。

两者怎么样? 精彩 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中,主要是因为函数的措辞更自然,这通常意味着更好的契合; - ) < / p>