title: How to avoid persisting entire aggregate root when adding child entity?
body: <p>I have a domain model with an aggregate root:</p>
  
  <pre><code>public interface IAggregateRoot {
      public string Id { get; }
  
      public string Foo { get; set; }
      public int Bar { get; set; }
  
      public IList&lt;IChildEntity&gt; Children { get; }
  }
  </code></pre>
  
  <p>The Children collection tends to get very large and I will be lazy loading it when an IAggregateRoot instance is retrieved through the IAggregateRootRepository. My problem is this; if I want to add an IChildEntity to an IAggregateRoot's Children collection, how can my repository allow me avoid persisting the entire aggregate?</p>
  
  <p>For example, let's say my IAggregateRootRepository were to look like the following:</p>
  
  <pre><code>public interface IAggregateRootRepository {
      public IAggregateRoot GetById(string id);
  
      public void AddOrUpdate(IAggregateRoot root);
  }
  </code></pre>
  
  <p>Then I could add to the Children collection by getting an IAggregateRoot instance through IAggregateRootRepository.GetById(), adding the child to the Children collection, then persisting it all through IAggregateRootRepository.AddOrUpdate(). That would, however, persist the entire aggregate root, along with its massive collection of children, every time I add a child entity. I guess I could get around that if my repository looked like:</p>
  
  <pre><code>public interface IAggregateRootRepository {
      public IAggregateRoot GetById(string id);
  
      public void AddOrUpdate(IAggregateRoot root);
      public void AddOrUpdate(IChildEntity child);
  }
  </code></pre>
  
  <p>But, as I have understood the repository pattern, a repository should only deal with aggregate roots and the solution above certainly breaks that requirement.</p>
  
  <p>Are there other, better, ways of avoiding this problem?</p>