I'm playing around with DDD in a node.js project and struggling with updates to child entities from the aggregate root.
For the sake of example, let's say I have two domain objects where Event
is my aggregate root:
class Event {
_id
_date
_comments = []
}
class Comment {
_id
_name
_content
}
I understand that DDD dictates that an EventRepository
is in charge of saving any changes to the AR and child entities and hides all the persistence details. So I should call something like EventRepository.save(event)
at this point and be done.
Let's say that an event currently has 3 comments and a user has edited the content of the second comment. The implication now is that my repository needs to fetch my event and comments and compare with my domain objects to figure out which need to be persisted. That's doable but seems inefficient when the reality is I know that only one comment needs to be updated. It would also be simple to add another repository for comments but now I've veered away from pure DDD. I've seen discussions of the Unit of Work pattern but I'm not sure how this ties into my repository. It would also be simple enough to just have some sort of _isDirty
on all my domain objects but now I've polluted them with persistence information.
I'm not sure it's relevant to the design pattern I'm struggling with but I'm doing this POC with objection.js and Postgres. Appreciate any feedback.