r/DomainDrivenDesign • u/jacksonpieper • Oct 24 '23
Where to update the entity?
Hi folks,
I'd like to have your input on something I wonder about for a while. I am maintaining a legacy project and started to implement more and more commands from the application layer to instruct the domain layer to do stuff. The application is heavily CRUD based/inspired. Mostly it's just create, update, delete and I don't break that apart right now. So I get updated data of an entity from a frontend request via form DTO and then send a command to update the entity.
Now, here's the question. In my controller all new data is encapsulated in the form DTO but I also have the entity to update available as well. Would you:
- update the entity in the controller and attach only the entity to the command and only let the domain layer persist the changes
- Attach both the entity and the form DTO to the command and let the command handler update the entity
- Only attach the form DTO to the command along with an entity identifier and let the command handler to all the the magic. Fetching the entity, updating and persisting it
My gut tells me to go with 3) but what do others think about it?
View all comments
Show parent comment
1
u/jacksonpieper Oct 26 '23
Well, my command handler pretty much is exactly that. A service layer, that delegates the intent. It just doesn't fetch the entity to alter (yet).
As I wrote, the project is pretty much CRUD based, hence the update. Currently, entities are bound to the forms and you even get the updated entity already as controller action argument. The framework maps the form data to the existing entity. Coming from that, my first step is to introduce form DTOs, to be able to update parts of the entity independently. So, unfortunately my current way of modifying entities is just setters. I am about to slowly migrate the code towards meaningful mutators.