领域驱动设计价值对象,如何保证价值的唯一性

发布于 2024-09-03 22:41:40 字数 244 浏览 1 评论 0原文

我正在构建一个问卷创建器。调查问卷由部分组成,部分由页面组成,页面由问题组成。问卷是聚合根。

部分、页面和问题可以具有所谓的短代码,这些短代码在调查问卷中应该是唯一的(但在数据库中不是唯一的,因此它们严格来说不是一个身份)。我打算使短代码成为一个值对象,并希望包含它在调查问卷中应该是唯一的业务规则,但我不确定如何确保这一点。我的理解是,值对象不应该访问存储库或服务层,那么它如何确定它是否是唯一的?

感谢您的任何帮助。

达伦

I am building a questionnaire creator. A questionnaire consists of sections, sections consist of pages and pages consist of questions. Questionnaire is the aggregate root.

Sections, pages and questions can have what are called shortcodes which should be unique within a questionnaire (but not unique within the database hence they are not strictly an identity). I intended to make the shortcode a value object and wanted to include the business rule that it should be unique within the questionnaire but I am unsure how to ensure that. My understanding is that the value object should not access the repository or service layer so how does it find out if it is unique?

Thanks for any help.

Darren

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

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

发布评论

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

评论(3

青丝拂面 2024-09-10 22:41:40

您可以将生成短代码的责任分配给调查问卷聚合。它可以基于嵌入到调查问卷对象中的简单计数器。每个想要创建短代码的实体都会调用其包含调查问卷的 NextShortCode() 方法来获取下一个唯一的(在当前调查问卷范围内)短代码值。

You could assign the responsibility of generating shortcodes to the questionnaire aggregate. It can be based on a simple counter embedded into questionnaire object. Every entity that want to create a shortcode would call its containing questionnaire's NextShortCode() method to obtain next unique (in scope of current questionnaire) shortcode value.

榕城若虚 2024-09-10 22:41:40

显然,您将需要数据库中的 PK 字段。如何实现取决于您,但如果是我,我会将 PK 字段携带到您的值对象中,以便自动处理它。

Clearly, you are going to need a PK field in the database. How you implement that is up to you, but if it were me, I'd carry the PK field into your value object so that it is handled automatically.

究竟谁懂我的在乎 2024-09-10 22:41:40

您可能需要使用 Guid 或 UUID 之类的内容。具体如何生成它取决于您的应用程序使用的语言。

You would want to use something like a Guid or UUID. Exactly how you would generate one would depend on what language you are using for your application though.

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