Consider the case of Student and Course.
The student can enroll the course only if the student's age is above the minimum age required to enroll that course.
From my point of view, Student and Course can be considered as aggregate where Student is the root entity, Course the child entity, and age the invariant to respect.
Student should have a method Student.SubscribeTo(Course course) and the method should enforce the invariant Student.Age >= Course.MinAge
and generate an exception otherwise.
Is this correct in the DDD approach? Or should I only pass to SubscribeTo only CourseId? Student.SubscribeTo(int CourseId)
From my point of view if there is no way to break the invariant, access to Course externally to the aggregate should be allowed. If I change the Course.MinAge in some other places of my code I don't break my business requirements, as I want the age be respected only when subscribing the course and I don't mind if later the Course.MinAge changes.
Different case if business requirements state: when Course.MinAge changes students already enrolled in the course should be removed from the course if Student.Age < Course.MinAge
.
Aucun commentaire:
Enregistrer un commentaire