Actively enforcing the aggregate root boundaries by setting the child entities constructor as Internal
Some more Aggregate Root thinking…
If a child entity in an Aggregate Root should not be accessible outside the boundaries of a Root, why should you be able to instantiate it?
If I mark their constructor with internal, it should be only instantiated from a Root (root should know how to instantiate it), this would also allow us to play with them only within the Model project, but others should get it via Root (like Services, Client applications and others).
Example…
public class Car : EntityBase<int>, IAggregateRoot
{
public Car()
{}
public Wheel RightWheel
{
get
{
return new Wheel(”Right”);
}
}
}
and Wheel (child entity):
public class Wheel : EntityBase<int>
{
private string _wheelType;
internal Wheel(string wheelType)
{
_wheelType = wheelType;
}
}
Again… my insecurities and concerns come from previous reasons as with the previous post:
Pros:
- Explicitness
- Active enforcing of rules
Cons:
- Cluttering the design
- Running with scissors… we like running with scissors, although we probably won’t fall on them
What do you think?
I am having double thoughts.


(2 votes, average: 4.5 out of 5)



