0

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.

Nick N
  • 49
  • 5

1 Answers1

0

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.

VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
  • 1
    Thanks for the reply. The factory makes sense in theory. But I still don't see how it fits into the DDD scenario. If my Aggregate had public properties, it would be easier. I suppose I could also have a special constructor on the Aggregate that bypasses all invariants. But I'm not a fan of that, since any consumer could then use it. – Nick N Mar 11 '22 at 16:38