Search code examples
architecturedomain-driven-designcqrs

How to refactor business logic to a third-party service call in a domain entity?


I have a domain entity, ProjectEstimate, that knows how to calculate its own estimated currency amount. It is was a simple calculation based on a number of fixed values available on the object itself. The Calculate method is called as part of the business logic and the EstimateCost field updated with the amount. Nothing too tough there.

However, now the calculation logic has dramatically increased in complexity and has been factored behind a third-party HTTP API call, and I am struggling to figure out how to make calls out to this API without violating the "purity" of the domain and its logic.

I am comfortable taking a double-dispatch approach to call a domain service, but even this leaves me with questions as to where I can put the logic to call the third-party API.


Solution

  • Double dispatch such as encapsulating with CalculationRequested and CalculationReceived is usually how any external calls should be encapsulated.

    In event modeling, it's explained by the "Todo list" pattern: https://eventmodeling.org/posts/what-is-event-modeling/#automation