I want to write a factory method to instantiate an entity that is an aggregate root.
Should the method accept the aggregated child entities and values as instantiated objects, or should it accept only primitive types?
For example, if I had an entity Computer composed of a Processor and a Memory object, should the factory method take the form:
public Computer NewComputer(
string computerName,
int processorCores,
int processorClockSpeed,
string memoryType,
int memoryRam)
{
...
}
or
public Computer NewComputer(
string computerName,
Processor processor,
Memory memory)
{
...
}
Is it only a matter of taste, or are there any serious considerations here?
It's just a matter of taste, though it can depend on your object-creation strategy, plus you might mix and match them.
Where your aggregate root already has factory methods for its child objects (e.g., if CreateProcessor() already exists to support adding additional processors), your first approach might be appropriate.
Alternately, if you're using a ComputerFactory (or repository) to create or reconstitute your aggregate root, that factory may already knows how to create child objects, in which case it will create them en route to building up your aggregate's graph and your second approach will be appropriate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With