2014-04-09 108 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