title: Aggregate Root in context of Repository Pattern
body: <p>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?</p>
  
  <p>Let me give an example.</p>
  
  <p>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</p>
  
  <pre class="lang-cs prettyprint-override"><code>class School
  {
      string SchoolName;
      IList&lt;Student&gt; students;
  }
  
  class Student
  {
      string StudentName;
      string Grade;
      School mySchool;
  }
  </code></pre>
  
  <p>School is the aggregate root here. Now suppose we want to define a repository for persistence operations.</p>
  
  <p>Which of below would be correct ?</p>
  
  <p>1)</p>
  
  <pre class="lang-cs prettyprint-override"><code>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
  }
  </code></pre>
  
  <p>2)</p>
  
  <pre class="lang-cs prettyprint-override"><code>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.
  }
  </code></pre>
  
  <p>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)</p>