Controlling state changes from the root object
Hey all. I have the following structure of objects, which basically consists of a
PhaseHolder
, which contains a collection of Phase
. Each Phase
can hold a Comment
, and each Comment
can hold a list of Reply
.
The PhaseHolder
object contains a bool
property that should control adding comments, but this can be avoided:
I could create an read-only IPhase
interface , but these models also serve as my EF core models, which need a concrete type, e.g. IReadOnlyCollection<Phase>
1 Reply
this is plausibly something you could prevent at the database level with a constraint or a trigger
or at the EF level by overriding one of its methods (interceptors etc)
but more generally this feels like a case where it would be worth separating your ef models from your domain objects
if you want to enforce this business concern in code
load your data with the ef models, immediately map them to in-app only classes which uphold this variant more strongly (runtime exceptions perhaps, or other methods - maybe nested classes would let the phaseholder control access to the phase comments)
i'm not familiar with DDD but it sounds like you essentially want your phase holder to be an aggregate root, so it may be worth looking up strategies DDD people use for implementing those