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?
An ‘aggregate root’ is the topmost node of an object graph. All other objects are created and accessed via this root object. In other words: If you create the components of the Computer class by an external factory method, then you cannot call it ‘aggregate root’ any more.
This is not to say that your second example is somehow bad or smelly or something, it’s just that it doesn’t meet the ‘aggregate root’ concept…