I know there are lots of questions talking about aggregates references here. But after reading some of them I still do not get an answer.
First I will describe my business logic.
I have two Entities:
Resource
which describe the resource of a computation unit, including cpu, gpu, memory, disk. Resource
can be used by users.
class Resource {
CPU cpu;
GPU gpu;
Memory memory;
Disk disk;
int totalCount; // The number of resource if limited
boolean isAvailable; // Resource can be unavailable
List<UserId> whiteLists; // Resource can give a white list where only limited users can use
}
Plan
is a merchandise. It has a price and bind with a Resource
and time.
class Plan {
String resourceId;
Money price;
int hours;
}
Right now I just save resourceId
in Plan
to follow the DDD principle: Aggregates should reference other aggregates by Id
. But here is some business logic which make it quite difficult:
Plan
should also follow theResource.whiteList
, it means that when aResource
is only accessful by some users then thePlan
using thisResource
should also follow thiswhiteList
.Plan
should not be availabe if itsResource
is not available.Plan
with the sameResource
(but have different time) should follow some variants. For example,Plan
with more time should always be expensive than thePlan
with less time.
This three logic all means that Plan
should always access Plan.Resource
. And I think this is not just the CQRS read model's logic which only show the Plan and Resource together. It is necessary even in some Plan
creation workflow.
So right now I think use Resource
in Plan
is quite necessary. But how to solve the conflict with the DDD principle, I just do not know how to explain this situation. Am I wrong to make them two separate aggregates? But the Plan
and Resource
do need to be editable separately.
Update
There are some details I did not describe well.
- The
Plan
will not be more than 100 during a very long lifecycle. - The
Plan
andResource
can only be edited by the administrator. There is no concurrency issue there. ThePlan
be bought buy users but users never edit it. - The
Plan
should always follow thewhiteList
inResource
, it is not acceptable that one user buy aPlan
which he / she even have no rights to use theResource
.