【发布时间】:2015-08-17 22:22:49
【问题描述】:
考虑以下场景:
public class Document
{
private ISet<User> sharedWith;
public Document(string name)
{
this.sharedWith = new HashSet<User>();
this.Name = name;
}
public string Name { get; private set; }
public IEnumerable<User> SharedWith
{
get
{
return this.sharedWith;
}
}
public void ShareWith(User user)
{
if (this.SharedWith.Contains(user))
{
throw new Exception("This document is already shared with that user.");
}
this.sharedWith.Add(user);
}
}
-
Documents可以与User共享 - 与用户共享文档时,如果文档已与该用户共享,则抛出异常。
- 文档可以与成千上万的用户共享。
显然这不能很好地扩展,因为需要检查 SharedWith 是否存在用户,导致 ORM 将整个集合延迟加载到内存中。我可以在应用程序服务中进行存在性检查,但是我考虑了这个域逻辑,所以我将它保存在 Document 类中对我来说是最有意义的。
我似乎无法弄清楚这应该如何使用 DDD 来完成?如果我无法使用 ORM,该怎么办?
我想我应该有一个 Document Aggregate 和一个 User Aggregate?
我查看了各种 DDD 资源(虽然我没有读过这本书),但我似乎无法找到这个特定场景的答案。
【问题讨论】:
-
这种类型的逻辑可以/应该属于域服务。只是处理此类事情的另一种战术模式。你的仓库可以有一个简单的方法返回一个关于文档是否已与特定用户共享的布尔值
-
@Marco 那么我无法理解域类本身应该采用什么样的逻辑?我描述的不是领域逻辑吗?
-
聚合强制执行的一致性边界是它的不变量。您的业务用户真的说这是业务规则还是您自己提出的?
-
您也可以使用双重调度,传入的用户可以包含与其共享的文档 ID 的集合,并且文档可以在那里进行检查
-
我想出了这个 -
the consistency boundary an aggregate enforces is its invariants不确定这是什么意思?
标签: c# .net orm domain-driven-design persistence