I have a cumulative root domain model:
public interface IAggregateRoot { public string Id { get; } public string Foo { get; set; } public int Bar { get; set; } public IList<IChildEntity> Children { get; } }
The Kids collection has a tendency to grow very large, and I will lazily load it when an IAggregateRoot instance is retrieved through the IAggregateRootRepository. My problem is this; if I want to add IChildEntity to the IAggregateRoot Children collection, how will my repository avoid me saving the entire aggregate?
For example, let's say my IAggregateRootRepository should look like this:
public interface IAggregateRootRepository { public IAggregateRoot GetById(string id); public void AddOrUpdate(IAggregateRoot root); }
Then I could add to the Children collection by getting an IAggregateRoot instance through IAggregateRootRepository.GetById (), adding a child to the Children collection, and then saving it through IAggregateRootRepository.AddOrUpdate (). However, this will persist throughout the aggregate root, along with its massive collection of children, every time I add a child. I think I could get around this if my repository looked like this:
public interface IAggregateRootRepository { public IAggregateRoot GetById(string id); public void AddOrUpdate(IAggregateRoot root); public void AddOrUpdate(IChildEntity child); }
But, since I understood the repository template, the repository should deal only with the aggregate roots, and the above solution certainly violates this requirement.
Are there any other better ways to avoid this problem?
source share