Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: Where should I set modified date and modified by? Repository or application service?

Where should I set fields like CreatedDate, CreatedBy, ModifiedDate, ModifiedBy? Should I pass current user context to repository and set it there or maybe better way is to set it in application service (but then it must be done in each API method rather than only in Add/Update in repository)?

like image 893
devkosiz Avatar asked Feb 07 '23 18:02

devkosiz


2 Answers

It depends on your domain.

If values like CreatedDate, CreatedBy... are for tracking or logging purpose, then I'd place them in the Infrastructure (Repository).

On the other hand, if these values belong to my domain for any reason, then I'd place them in the domain layer.

Example: imaging that in a Banking Transfer context, a customer could cancel a transfer only until 24 hours after submit it for settlement. Then the domain needs CreateTransferDate to satisfy the invariants.

Another option could be a listener that consume all domain events and save historial time data of what happen.

like image 81
martinezdelariva Avatar answered Feb 09 '23 08:02

martinezdelariva


CreatedDate, CreatedBy, ModifiedDate and ModifiedBy are typically concepts that have no real domain value, but they are more like a technical concept; and that's why I typically don't set these in application or domain layer, but in repository layer. After all, the repository layer is a technical layer. In a domain I don't care about this kind of information. Unless of course, it's part of the ubiquitous language, but mostly it's not.

Also, if ModifiedDate would be part of the domain/application, you would have to set it with each manipulation you do, which would be very tedious and error-prone. If you do it in the repository, it's easier, because you do it with every update.

So I would ask the question: does business care about it?

like image 38
L-Four Avatar answered Feb 09 '23 06:02

L-Four