通过域封装持久性,还是通过存储库持久性?

发布于 2024-10-05 11:11:56 字数 389 浏览 8 评论 0原文

如果我的域模型不应该知道/关心存储库,那么像 .UpdateOrder(...) 这样封装 CRUD 更新的行为如何与存储库交互?通过域服务?

好的,那么我的存储库有一个有效的 CRUD 更新,它与我的 .UpdateOrder(...) 结合使用。没关系。但我不希望有人在存储库上使用 Update 方法,我希望他们检查实体上的行为(改为使用 UpdateOrder() )。我更喜欢类似于我的域模型满足不变量的方式 - 通过它的设计(私有集属性等) - 我的存储库公开“更新”/持久化实体的替代方法。

这只是一个访问修饰符问题吗?我在 Repo public 中没有 Update 方法就解决了这个问题。或者有一个“更好”的答案吗?请 DDD 忍者帮助我。

If my Domain Model is not supposed to know/care about the Repository, then how does some behaviour like .UpdateOrder(...), that encapsulates a CRUD-Update, interface with the Repository? Through a Domain Service?

Ok, then my Repository has an effective CRUD-Update that's used in conjunction with my .UpdateOrder(...). That's fine. But i don't want someone to use the Update method on the Repository, i want them to go through the behaviour on the Entity (use UpdateOrder() instead). I'd prefer that in likeness to the way my Domain Model satisfies invariants - by it's design (private set properties, etc) - my Repository not expose an alternate method to "updating"/persisting the Entity.

Is this simply a access modifier problem that is solved by me not having the Update method in the Repo public. Or is there a 'better' answer? Please help me DDD ninjas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪推晚风 2024-10-12 11:11:56

DDD 中的严格顺序是:

var entityRepository = MyServiceLocator.Get<IEntityRepository>();
var myEntity = entityRepository.Load(<some criteria>);
myEntity.Change(something);
entityRepository.Save(myEntity);

存储库始终负责检测/保留实体内的所有更改。

(顺便说一句,我假设你的实体是一个聚合根)

The strict sequence in DDD would be:

var entityRepository = MyServiceLocator.Get<IEntityRepository>();
var myEntity = entityRepository.Load(<some criteria>);
myEntity.Change(something);
entityRepository.Save(myEntity);

The repository is always responsible for detecting/persisting all of the changes within the entity.

(btw, I'm assuming that your entity is an aggregate root)

怼怹恏 2024-10-12 11:11:56

如果您的领域模型不包含持久性,那么它就不包含存储某些内容的操作。如果您的实体是来自领域模型的东西,那么它就没有必要持久化自己。

你说:

没关系。但我不想要一个人
使用 Update 方法
存储库,我希望它们经过
实体上的行为

但我认为这是错误的。您的域对象没有比打印自身、在屏幕上绘制自身等更多的责任来保留自身。您的域类不应该有 UpdateOrder 方法。

现在,您可能不想将原始存储库(从持久性实现层)公开给其他代码,但这只是意味着将其包装在合适的东西中。听起来您确实有需要讨论持久性的代码,因此请弄清楚它需要在什么级别的讨论上工作,并为其公开一个合适的接口。

If your domain model doesn't include persistence, then it doesn't include the operation of storing something. If your entity is something from the domain model, then it has no business persisting itself.

You say:

That's fine. But i don't want someone
to use the Update method on the
Repository, i want them to go through
the behaviour on the Entity

But i think that's mistaken. Your domain objects have no more responsibility for persisting themselves than they do printing themselves, drawing themselves on screen, etc. Your domain class should not have a UpdateOrder method.

Now, you might not want to expose the raw repository (from your persistence implementation layer) to other code, but that just means wrapping it in something suitable. It sounds like you do have code that needs to talk about persistence, so figure out what level of discourse it needs to work at, and expose a suitable interface to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文