【发布时间】:2011-11-28 07:19:40
【问题描述】:
我有一个实体,它是一个聚合根并包含许多子实体。基本上,从数据库中加载它并将其全部持久化是一项非常昂贵的操作。大多数时候,我只更改实体的一小部分,因此实际上不需要加载和持久化整个实体。但是,我不确定如何使用 DDD 原则和存储库模式来实现这一点。
我一直在考虑类似的事情:
interface IAggregateRoot {
string Id { get; }
ISubEntityA EntityA { get; }
IList<ISubEntityB> EntityB { get; }
}
interface ISubEntityA {
string Id { get; }
int Foo { get; set; }
}
interface ISubEntityB {
string Id { get; }
string Bar { get; set; }
}
interface IRepository {
IAggregateRoot Get(string rootId);
ISubEntityA Get(string rootId);
IList<ISubEntityB> Get(string rootId, int offset, int pageSize, out int numPages);
ISubEntityB Find(string rootId, some specification to select a single ISubEntityB);
// I can update either the entire aggregate root or an individual sub entity using
// these methods.
void AddOrUpdate(IAggregateRoot root);
void Update(string rootId, ISubEntityA a);
void Update(string rootId, ISubEntityB b);
}
这种方法有什么问题吗?有没有关于这个“问题”的最佳实践?
【问题讨论】:
标签: c# domain-driven-design repository repository-pattern ddd-repositories