Question

I am using Python 2.7.2 and SQLAlchemy 0.9.3. A Department has many groups.

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'))
    ...

Where do get_groups and add_group methods belong? In DepartmentService or 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):
        ...

Or do I call the GroupService from DepartmentService ?

Was it helpful?

Solution

This answer is purely my personal opinion. There may be a better technical reason I'm not aware of.

How about both? The great backref feature will keep the database in sync for you:

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):
        ...

If I had to choose one, I'd put it in GroupService, mainly because the wording of the functions are more natural, which usually means a better fit ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top