I have two entities User and Course. A user can take several courses which makes the relationship one to many. But a single course can be taken by many students so it makes it many to many relationship.
Now, I need to register a course for a user. My user entity has:
public void AddCourse(Course course)
{
if (CoursesAlreadyAdded(course))
{
AddBrokenRule(new BrokenRule() { PropertyName = course.ClassCode, Message = String.Format("Course with classCode = {0} already added", course.ClassCode) });
return;
}
UserCourses.Add(new UserCourse() { UserId = this.UserId, CourseId = course.CourseId, Course= course, User = this});
}
The classes are generated through Linq to SQL. Linq to sql is not capable of performing many to many relationships so I have to handle it myself.
Now, the question is that how will I send the information to the database. Should UserRepository.Save(user) be responsible for saving the courses.
This kind of means that User is aggregate root of Course entity but in real it is not since I can access Course many different ways and I am not dependent on the User object to provide me Course.
Even if I have a CourseRegistrationService (which I have) I have to call a repository to persist the changes. Which repository is responsible for persisting the changes about user and course relationship. Maybe a UserCourseRepository!!
Also, just by putting a List under User object makes User an aggregate root or is that incorrect. If so then how would you design application using OR MAPPERS which generates List and one to many relationships automatically.