The Dilemma of Responsibility
One of the things that bug me while developing a new software is the issues related with distributing the responsibility among entities and managers. Let's say that I have an Exam object which has a IList of Questions. The relationship between the two is one-to-many. The collection Questions is set to be in lazy mode. This means the collection Questions will only be fetched when required. I have an ExamManager which works with Exam entities and QuestionManager which works with Question entities.
The ExamManager class have a method GetById(int id) which returns an Exam based on the id passed. This method should return the Exam object with the Questions collection. The code is given below:
public static Exam GetById(int id)
{
Exam exam = null;
using (ISession session = sessionFactory.OpenSession())
{
exam = session.CreateCriteria(typeof(Exam))
.SetFetchMode("Questions", FetchMode.Join)
.List<Exam>()[0];
}
return exam;
}
Although the code works fine but the responsiblity is distributed. Now, the ExamManager is fetching the questions and populating the exam object. I can use the QuestionManager to get all the questions based on the exam but then exam.Questions is a readonly collection hence it cannot be assigned.
Off course, I can use a method like Populate/Insert Questions into Exam but this just does not sound right. My main concern is that should the responsibilty of fetching the questions of the exam be inside the ExamManager or should this be part of the QuestionManager?