If I have two classes [Shift,ShiftDetails] where the [Shift] is the aggregate root. based on a specific field I want to constraint the number of instances of ShiftDetails.
I create a ShiftDetailsView class then create two methods in Shift aggregate root to control the invariant:
- public IEnumerable ConstructShift();//based on the NumberOfSuccessions field
This method Should create number of intialized ShiftDetailsView based on that field and then add them to a list and return the result back to user(developer) as IEnumerable.
Then the user should call CompleteShift after filling the intial IEnumerable returned from the previous method:
- public List CompleteShift(IEnumerable shiftDetailsViews)
I did two separate steps to control the number of childs through the aggregate root and I think there are better ways to do that to guarantee the (ACID) of the whole thing.
Based on the comments, The question needed more clarification, and because I try to simplify the problem, I made a mistake and change the real problem. because when we treat with domain problems we have to be accurate and clarify the exact problem. So I will try to explain it in more detail.
I have the following Two aggregates:
1. First Aggregate
1-WorkingSystem: (aggregate root)
private readonly ICollection<WorkingTime> _assignedWorkingTimes;
public string Name { get; private set; }
public short NumberOfSuccessions { get; private set; }
public Week WeekStart { get; private set; }
public bool IsActive { get; private set; }
public bool IsDefault { get; private set; }
public short NumberOfWeekends { get; private set; }
public virtual ICollection<WorkingTime> AssignedWorkingTimes { get => _assignedWorkingTimes; }
Example:
Id |Name | NumberOfSuccessions|WeekStart|IsActive|NumberOfWeekends
1 |Employees| 2 |Sunday | 1 | 2
2 |Lecturers| 1 |Saturday | 1 | 1
2-WorkingTime:
public string Name { get; set; }
public short NumberOfHours { get; set; }
public int WorkingSystemId { get; private set; }
Example:
Id|Name | NumberOfWorkingHours | WorkingSystemId
1|Summer | 8 | 1
2|Winter | 6 | 1
3|General| 8 | 2
2. Second Aggregate
3-Shift (aggregate root).
private readonly List<ShiftDetail> _assignedShiftDetails;
public string Name { get; set; }
public ShiftType ShiftType { get; set; }
public int WorkingSystemId { get; set; }
public virtual WorkingSystem WorkingSystem { get; set; }
public virtual IEnumerable<ShiftDetail> AssignedShiftDetails { get => _assignedShiftDetails; }
Example:
Id|Name |ShiftType | WorkingSystemId
1 |restaurant-shift|Morning | 1
4- ShiftDetails:
public Guid ShiftId { get; private set; }
public int WorkingTimeId { get; set; }
public DateTimeRange ShiftTimeRange { get; set; }
public virtual WorkingTime WorkingTime { get; set; }
Example:
ShiftId = 1|WorkingTimeId = 1|ShiftStart = 8:00|ShiftEnd = 16:00
ShiftId = 1|WorkingTimeId = 2|ShiftStart = 9:00|ShiftEnd = 15:00
Now I want to constraint the number of details based on the NumberOfSuccessions for each WorkingSystem! and because I have an access to WorkingSystemId in my aggregate root Shift then I can access this info.
Could you help me to control the number of instances based on a field in the aggregate root ?
ConstructShift()? Do you have to setup some kind of template to be filled based on the currentshiftinstance or it's just a matter of letting the client know how many shifts must be provided? - plalxshift) in my example should control the Invariant (the number of shift details) in my example for the system to be in a consistent state. So I create this method for the client to call it and it will take care of returningIEnumerable of shiftDetailsViewsit's initialized by the correct number of emptyshiftDetailsViewsthen the client can fill this initial templateIEnumerablethrough looping on it and setting their values then callCompleteShift(IEnumerable shiftDetailsViews)at the end. - Anyname Donotcare