5

According to DDD (Blue book, Evans) a Factory has the responsibility to create an Aggregate Root in a valid state. Does this mean it should be able to create the technical id (objectId in mongoDB world) as well as the domain id?

On the one hand, this seems like a technical detail and it would seem okay to let Mongo handle the creation of the ID.

On the other, enabling querying by id (by having getById in a DDD repository) exposes the technical id to the domain, which in turn would make it the responsibility of the Factory to create it.

Perhaps I can't get my head on the different use-cases / overlap, etc. of Technical Id's vs DomainId's or perhaps I'm being overzealous, but I'd appreciate your opinion anyway.

In short: In DDD: Should a factory be able to create the technical Id as well as the domain Id?

possible implementation: Hi/Lo ( How to set the hilo sequence starting value in MongoDB Norm?)

EDIT: although the hi/lo way exposes the Factory to the persistence layer, which is something only the Repository should know. hmmm

Thanks

Community
  • 1
  • 1
Geert-Jan
  • 18,623
  • 16
  • 75
  • 137
  • Minor not entirely relevant comment; MongoDB doesn't actually create the IDs, the client (driver) does (with the exception of upsert operations). – Remon van Vliet Aug 03 '12 at 16:06

1 Answers1

3

Factories don't have to concern themselves with the ID because the validity of an aggregate is orthogonal to identity. Identity can be assigned in a few different ways, either as a incremental ID from a relational database in which case the repository has to manage it, or as a UUID/GUID in which case it can be assigned by the factory, or repository, or even the calling client which is convenient because then the client has the key by default.

Whenever possible, I try to maintain a single identity for aggregates. I'm not sure if MongoDB requires an additional technical ID, but if it does and the domain ID can't be used in its place, then MongoDB should manage it on its own and behind the scenes.

eulerfx
  • 36,769
  • 7
  • 61
  • 83
  • 'the validity of an aggregate is orthogonal to identity.'. Surely in some cases an aggregate must have an identity before it can be called valid? – Geert-Jan Aug 03 '12 at 09:29
  • But indeed only a domain identity (which should be set in the factory right ? ) and not à Technical id. Still can't get my head around why it's preferred ( though not required) to have a separate Technical id in mongodb, but i guess that's an entirely different question – Geert-Jan Aug 03 '12 at 13:13
  • An assigned identity value can indicate that an aggregate is persistent, but validity is different. I certainly see the relationship, I just think that identity gets special treatment in this regard... – eulerfx Aug 03 '12 at 15:24