领域驱动设计 - 重组工厂如何绕过不变量?

发布于 2025-01-11 11:22:18 字数 401 浏览 1 评论 0原文

我在弄清楚重构工厂与 DDD 配合使用时如何工作时遇到了一些困难。

据我了解,该工厂将由存储库层(或者更确切地说,不是域)拥有并被简化,因为它期望存储的所有实体都已经有效。

我感到困惑的是,位于域外层的工厂如何知道如何创建域对象,同时基本上绕过任何不变检查?我只在聚合上打开特定方法以允许创建/更新该实体。例如,调查中有一个人员列表,我公开了一种将人员添加到调查中的方法(域检查以确保您不会多次添加 1 个人)。我没有其他方法可以将人员添加到该调查汇总中。

如果有帮助,我考虑此选项的原因是因为在发送调查后,我想在将已收到调查的人和未收到调查的人添加到调查汇总时区分他们。这样,当我发送给新人时,我知道谁应该接收它,而不是那些已经收到它的人。在添加人员时,我可以允许一个可为空的时间参数,但我觉得这没有充分传达意图。

感谢您的阅读。

I'm having some difficulties figuring out how a reconstitution factory works when paired with DDD.

From what I understand, this factory would be owned by the repository layer (or rather, not the domain) and is simplified, as it expects all entities stored to be valid already.

Where I get confused, is how could a factory that lives in a layer outside the domain, know how to create a domain object while essentially bypassing any invariant checks? I only open specific methods on the aggregate to allow creation/updating of that entity. For example, a Survey has a list of people on it and I expose a method to add a person to the Survey (domain checks to make sure you're not adding 1 person multiple times). I have no other way of adding people to that Survey aggregate.

If it helps, the reason I'm considering this option is because after I send a Survey, I want to differentiate people who have received the Survey and those who haven't when adding them to the Survey aggregate. That way when I send to new people, I know who should receive it vs those who have already received it. I could just allow a nullable time parameter when adding a person, but I felt like that did not adequately communicate intent.

Thanks for reading.

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

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

发布评论

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

评论(1

饮惑 2025-01-18 11:22:18

重组工厂如何绕过不变量?

对于工厂来说,通常的答案是它不使用域方法来初始化聚合,而是通过构造函数将信息复制到聚合中。

换句话说,工厂的职责就是构造对象图,除此之外别无其他。它从数据模型中取出信息,将其复制到值对象中,初始化实体,并确保新的内存表示形式与存储聚合时的状态相同。

How does a reconstitution factory bypass invariants?

The usual answer for a factory is that it doesn't use the domain methods to initialize the aggregate, but instead copies information into the aggregate via constructors.

In other words, the responsibility of the factory is to construct the object graph, and nothing else. It takes information out your data model, copies it into value objects, initializes entities, and ensures that your new in memory representation is in the same state as when the aggregate was stored.

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