2014-04-09 5 views
1

저는 Python 2.7.2와 SQLAlchemy 0.9.3을 사용하고 있습니다. 부서에는 여러 그룹이 있습니다. get_groups 및 방법이 속한 add_group을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')) 
    ... 

? 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