views:

77

answers:

3

There are four classes:

StudentBase, CourseBase and StudentDataMapper, CourseDataMapper

An Student object can have a relationship with an Course object. One Student can have many courses. One course can be visited by many students.

In the ER diagram, a Student entity has an attribute called "courses", but a course does not know anything about his students. A course has no Attribute called "students" in return.

Which of these classes should perform the creation of the relationship?

EDIT: This is the System Layer! In the Business Logic Layer, the developer subclasses StudentBase and CourseBase to create a Student class and an Course class. The developer who creates these classes will not see any of that code, except for his own business logic code.

A: 

Not 100% sure if I understand your question.

For instance, NHibernate (which is a object relational mapper) tries not to influence your business code as much as possible.

So the ORM should not manage your relations. It should take it as it is when storing, and recreating it to its original state when loading from the database.

If the course does not know anything about students, it can not manage the relation.

So based on your description of the situation, the student is the most natural place where the relation could be managed.

Stefan Steinegger
Sorry, I missed something. Updated. I agree with your Student assumption. Makes sense!
openfrog
A: 

To answer your question with regards to which one should manage the relationship, the answer could be Student, Course, or Both.

How do you see these classes being used? If they are always used from the Course perspective then you need to add a students property which manages the relationship. Conversely if they are always used from a student perspective then obviously that is where it lives.

However, if it could be both which seems the most likely answer given the types of data you are talking about, then it belongs in both places.

Chris Lively
+2  A: 

Neither.

First, the ERD should change; what you've described is a many-to-many relationship (a course can have many students and a student many courses). So, the student shouldn't have a courses attribute but, instead, there should be a student-course entity.

Then there should be a StudentCourseDataMapper class that does what you're asking (maintaining relationships) and both StudentDataMapper and CourseDataMapper should be aware of and use it.

Alkini