architecturedomain-driven-designcqrsevent-sourcingevent-driven

Commands that need information from external systems


Let's say I'm implementing an application that manages stocks and portfolios using event sourcing. I have the following commands:

The handling of the last three commands needs the stocks' prices. I'm receiving the commands through an API. Should I accept the stocks' prices within the DTO coming from the API, or should I retrieve it in a service component before passing the command to the event-sourcing core of the application?


Solution

  • Should I accept the stocks' prices within the DTO coming from the API

    Are the messages coming to you from a trusted source, that would never provide information that differs from what you would collect yourself?

    Then you can consider it.

    In most cases, you would want to fetch the values you need yourself. Ideally, you would fetch the information where you handle your I/O, and pass the information to your domain logic (which, fundamentally, doesn't care where the information comes from).

    In some cases, you won't necessarily know what information the domain logic is going to need, so you either (a) pass the capability to ask for the information to the domain logic -- usually in the form of a "domain service" or (b) design a protocol so that your domain layer can ask for information as it needs it, and let the application code figure out where to get it.