Search code examples
microservicesdomain-driven-design

How To get data from other microservice in ddd?


I have two services order service and Z service (Z is a hypothetical name). I Have 3 layer in my project (domain, application, infrastructure). The order service has a registration API. The user calls this web service along with a mobile number and other data. In ordering service, in order to receive the Z_ID, this phone number needs to be sent to the order service

enter image description here

I have a method in infrastructure layer for call Z service. But my question is in which layer should I call this method?

Should it be handled as follows in the application layer:

$order = Order::Create($price, $shop);
$Z_id = // call method in infrastructure
$order->setZID($zid);

or the domain layer must request to the infrastructure layer?


Solution

  • You can solve this problem in the following way:

    1. Declare domain interface (repository building block)
    2. Implement the interface in the infrastructure
    3. Use it in your application layer as an ordinary repository building block

    It is worth noting that in this approach domain isn't dependent on infrastructure anymore and as a result it gives you significant flexibility - for example it is easier to change microservice or even use DB instead of HTTP call, this attitude is called hexagonal architecture.