I have these classes:
public class Area{
...
private List<WorkSpace> _workspace;
public IReadOnlyCollection<WorkSpace> workspace;
}
and
public class WorkSpace{
...
private List<WorkSpaceReservation> _workspaceReservation;
public IReadOnlyCollection<WorkSpaceReservation> workspaceReservation;
}
and
public class WorkSpaceReservation{
...
}
Area is the aggregate root.
Now I want to add WorkSpaceReservation
to WorkSpaceReservation list. Is it possible in DDD context and rules to define a method in workspace like this:
public void AddWorkSpaceReservation(WorkSpaceReservation w){
_workspaceReservation.Add(w);
}
Or we should add this method in aggregate root?
If for isolation, we should add it in aggregate root how can I access to _workspaceReservation
from aggregate root?
There are arguments to avoid having the method in the Workspace
but as you mentioned add it in the Area
.
Having mutations in the aggregate root allows to implement invariants (validation) that can take into account the state of the entire aggregate, instead of just the single entity.
But doing it or not could not have a big impact depending what you need to do, more important is that also if the method is on the Workspace
you don't allow the Workspace
itself to be persisted, but only the aggregate root can be.
To solve the issue of the _workspaceReservation
field I think you have to do something with the access modifiers (maybe having it internal
?), but don't know much of C#