I’m using a hexagonal/clean/ports-and-adapter architecture. For the sake of simplicity, I’ll only talk about domain and infrastructure here, where the infrastructure has dependencies on the domain but the domain is unaware of the infrastructure.
My application (infrastructure) receives events. The infrastructure layer maps them to the domain objects and calls a use case. The use case uses the data from the domain object to calculate something and then saves it back.
As the domain layer is unaware of the infrastructure, it only calls a
save(domain object) method in an interface.
I implemented the interface in the infrastructure layer and uses a real database.
So far so good, but I’d also like to save some metadata from the event along with the domain object. Let’s say I need the event timestamp in order to avoid outdated events overriding newer data.
How can I save the metadata without polluting my domain layer? The event timestamp is not part of the domain; the domain should not know about events at all.
My current “solution” is not to call a
save method in the use case. Instead, the use case returns the updated domain object and the use case caller (infrastructure), then maps the domain object back to the database entity and adds the additional information from the event.
In my mind, that’s not a suitable solution. Saving should be part of the use case.
Any suggestions on this?