我有一個域模型與聚合根:如何避免在添加子實體時持久化整個聚合根?
public interface IAggregateRoot {
public string Id { get; }
public string Foo { get; set; }
public int Bar { get; set; }
public IList<IChildEntity> Children { get; }
}
兒童收集往往會非常大,我會延遲加載它,當一個IAggregateRoot例如通過IAggregateRootRepository檢索。我的問題是這樣的;如果我想將IChildEntity添加到IAggregateRoot的Children集合中,我的存儲庫如何避免保留整個集合?
例如,假設我IAggregateRootRepository分別來看看如下所示:
public interface IAggregateRootRepository {
public IAggregateRoot GetById(string id);
public void AddOrUpdate(IAggregateRoot root);
}
然後,我可以通過打通IAggregateRootRepository.GetById()的IAggregateRoot實例添加到Children集合,增加孩子的子集合,然後通過IAggregateRootRepository.AddOrUpdate()持久化。但是,每當我添加一個子實體時,它就會堅持整個聚合根,連同其龐大的子集。我想我可以得到解決,如果我的倉庫看起來像:
public interface IAggregateRootRepository {
public IAggregateRoot GetById(string id);
public void AddOrUpdate(IAggregateRoot root);
public void AddOrUpdate(IChildEntity child);
}
但是,正如我已經明白了存儲庫模式,資源庫應該只處理總根,上面肯定的解決方案打破了要求。
還有其他更好的方法可以避免這個問題嗎?
如何獲取兒童的名單?它是原始的AggregateRootRepository還是新的ChildAggregateRepositroy的工作?或者這是否意味着子集合現在需要知道如何在父實體中加載自己? – Eatdoku 2012-09-28 22:54:30