2014-04-09 163 views
1

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

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?

回答

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,主要是因为职能的措辞更加自然,这通常意味着更合适;-)

+0

我必须选择一个。现在我将放入GroupService并将其注入到DepartmentService中。 是的,backref是惊人的:D – Akash