通过存储库仅保留大型实体的一部分
我有一个实体,它是一个聚合根并包含很多子实体。基本上,从数据库加载它并持久化它是一个非常昂贵的操作。大多数时候,我只更改实体的一小部分,因此确实不需要加载和持久化整个实体。然而,我不确定如何使用 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);
}
这种方法有什么问题吗?关于这个“问题”有最佳实践吗?
I have an entity which is an aggregate root and contains a lot of sub-entities. Basically, loading it from the database and persisting it all is a very expensive operation. Most of the time, I only change small parts of the entity so there really is no need to load and persist the entire entity anyway. I am, however, not sure how to implement this using DDD principles and the repository pattern.
I have been considering something like:
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);
}
Are there any problems with this approach? Is there a best practice regarding this "problem"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用类似的技术。例如,假设我激活一个帐户:
由于您不希望您的存储库涉及域行为,因此您不会执行以下操作:
但您可以这样做:
因此您可以选择一种您喜欢的机制。
但回到你的存储库:我不会进行查询。轻量级查询层可以返回像 DataTable 这样简单的东西供前端使用;其他 DTO。这与您去那里的寻呼有关。
I use a similar technique. For instance, say I activate an account:
Since you don't want your repository involved with domain behaviour your wouldn't do the following:
But you could do this:
So you can choose a mechanism you are comfortable with.
But getting back to your repository: I wouldn't do querying. A lightweight query layer can return something as simple as a
DataTable
for your front-end to consume; else DTOs. This relates to the paging you have going there.该存储库仅处理聚合根,而不处理其部分。所以我建议这个
The repository deals only with aggregates roots and not with parts of it. SO I suggest this