为DDD中的实体生成身份 [英] Generating identities for entities in DDD

查看:255
本文介绍了为DDD中的实体生成身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始版本可以在修订版本下找到。

To further clarify my initial problem, I rewrote the question with more 'DDD'-termini, common patterns and discussion arguments. The orginal version can be found under revisions.


当正确应用DDD时,在域内生成 /

Where and how are identities for entities/aggregate roots being generated inside the domain when correctly applying DDD?

我需要在创建或持久化时为我的实体分配唯一身份。

I need to assign unique identities to my entities, either upon creation or persisting. Those identities can come in several styles


  • 计算(根据实体的特征,因此根据业务需求)

  • 自然(基于一组规则,因此基于业务逻辑)

  • 代理(基于随机生成的无业务价值)

使用工厂创建身份,授权到基础设施,有许多方法可用于生成和分配身份使用 ORM 数据库生成等。但是,如果正确应用DDD,应该在何处生成身份,并考虑到我们不希望贫血域模型将服务注入实体

There are many approaches for the task of generation and assignment of identities, from using factories to create identities, delegation to the infrastructure using ORM or database generation etc. However, if correctly applying DDD, where and how should identities be generated, considering that we don't want anemic domain models and injection of services into entities?


  • 没有贫血域模型

  • 没有依赖注入服务到授权


  • 工厂

  • 双重发送(可以用于身份生成吗?

  • 在知识库中生成

  • 内部基础设施(例如ORM或数据库)

  • 将服务注入实体

  • Factories
  • Double dispatch (can this be used for identity generation?)
  • Generation inside repositiories
  • Generation inside infrastructure (e.g. ORM or database)
  • Injection services into entities

推荐答案

我会把它放在工厂里。在我看来,生成id不应该是域逻辑的一部分,因为它真的是一个基础设施问题。您可以从DB获取ID或使用uuid或其他任何内容生成。这是一个细节。还要记住,只有工厂的界面属于域层,而不是其实现。

I would place it in a factory. Generating id shouldn't be a part of domain logic in my opinion, because it's really an infrastructure matter. You can take id from DB or generate it with uuid or whatever. It's a detail. Also remember that only interface of a factory belongs to domain layer, not its implementation.

关于您对工厂的怀疑,如果您使用工厂创建实体,那么您应该在任何地方使用它。这是我如何做的。

About your doubts for factory, if you use factory to create entities then you should use it everywhere. This is how I do it.

这篇关于为DDD中的实体生成身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆