I understand that Aggregate Roots are the only object that will be loaded by the client and all operations for the objects within the aggregate root are done by the Aggregate Root. By the same convention, there should one repository interface defined for an aggregate root and any persistence operations for any object within the aggreate root should be done by this "Aggreate Root Repository" corresponding to the Aggregate Root. Does that imply that only the Aggregate Root object should be passed to the "Aggregate Root Repository" for operations related to sub objects of the Aggregate Root?
Let me give an example.
Suppose you have a School object and Student Object. Since Student can't exist without a School, (you might say that a student might have left school, in this case he/she is no longer a student), so we have
class School
{
string SchoolName;
IList<Student> students;
}
class Student
{
string StudentName;
string Grade;
School mySchool;
}
School is the aggregate root here. Now suppose we want to define a repository for persistence operations.
Which of below would be correct ?
1)
interface ISchoolRepository
{
void AddSchool(School entity);
void AddStudent(School entity); //Create a School entity with only the student(s) to be added to the School as the "students" attribute
}
2)
interface ISchoolRepository
{
void AddSchool(School entity);
void AddStudent(Student entity); //Create a Student entity. The Student entity contains reference of School entity in the "mySchool" attribute.
}
In 1) we are only exposing the aggregate in the interface. So any DAL that implements the ISchoolRepository will have to get the Student object from the School object for adding a student. 2) looks more obvious and I may look stupid by suggesting 1) but the concept of aggregate root in pure theory would suggest 1)