title: How to design self referencing aggregate in Domain Drive Design
body: <p>Im working on a Task System where a Task can have collection sub-tasks and sub-tasks can have collection of sub-tasks and so on (recursive).</p>
  
  <p><code>DOMAIN</code></p>
  
  <p>The <code>task</code> are given based on <code>Organizational Chart</code></p>
  
  <p>Example Org Chart:</p>
  
  <pre><code>Mahdi
  ---Saeed
  ------Jaime
  ------Ahmed
  ---Tarawneh
  ------Mae
  ---Rasheed
  </code></pre>
  
  <p>The upper person on the Org Chart will assign task to the person under him.</p>
  
  <p>Lets say Mahdi will assign task to Saeed named <code>prepare course materials for IELTS</code>.</p>
  
  <p>Then Saeed might divide the task into sub-tasks.</p>
  
  <pre><code>prepare course materials for IELTS
  ---issue laptop and equipment (assigned to Jaime)
  ---prepare the checklist form(assigned to Ahmed)
  </code></pre>
  
  <p>Then in case its really a big task, Jaime might divide it further into sub-tasks.</p>
  
  <p>As per domain experts, its usually under 3levels</p>
  
  <p>Invariants:</p>
  
  <ul>
  <li>when moving the deadline of task, it should check that it should not exceed the deadline of its parent task</li>
  <li>if the task has sub-tasks, it will base its status on them. (the task will remain pending until there is one sub task that is pending.... the task will be automatically marked as done when all its sub tasks are done)</li>
  <li>each individual task can update their status if they dont have sub tasks</li>
  </ul>
  
  <p><strong>EDIT</strong></p>
  
  <ul>
  <li>I can only update task status that are assigned to me or I assigned to.</li>
  <li>I can only give tasks to staff that are directly under me</li>
  </ul>
  
  <p>Do I have to stick with the <code>Task</code> concept or there are concepts that Im still missing like MainTask &amp; SubTask (just an example)?</p>
  
  <p>If I will stick to the <code>Task</code> concept, should I load the whole graph or only the direct parent and child?</p>
  
  <p>OR should I just delegate all the work to a Domain Service? which might turn the Task to anemic model?</p>