【发布时间】:2012-01-21 21:47:48
【问题描述】:
我有一个带有聚合根的域模型:
public interface IAggregateRoot {
public string Id { get; }
public string Foo { get; set; }
public int Bar { get; set; }
public IList<IChildEntity> Children { get; }
}
Children 集合往往会变得非常大,当通过 IAggregateRootRepository 检索 IAggregateRoot 实例时,我将延迟加载它。我的问题是这个;如果我想将 IChildEntity 添加到 IAggregateRoot 的 Children 集合中,我的存储库如何让我避免持久化整个聚合?
例如,假设我的 IAggregateRootRepository 如下所示:
public interface IAggregateRootRepository {
public IAggregateRoot GetById(string id);
public void AddOrUpdate(IAggregateRoot root);
}
然后我可以通过 IAggregateRootRepository.GetById() 获取 IAggregateRoot 实例来添加到 Children 集合,将子项添加到 Children 集合,然后通过 IAggregateRootRepository.AddOrUpdate() 将其全部持久化。但是,每次我添加一个子实体时,这都会保留整个聚合根及其大量子元素。如果我的存储库看起来像这样,我想我可以解决这个问题:
public interface IAggregateRootRepository {
public IAggregateRoot GetById(string id);
public void AddOrUpdate(IAggregateRoot root);
public void AddOrUpdate(IChildEntity child);
}
但是,正如我所理解的存储库模式,存储库应该只处理聚合根,上面的解决方案肯定会打破这个要求。
还有其他更好的方法来避免这个问题吗?
【问题讨论】:
标签: c# domain-driven-design repository repository-pattern