1

Let's say I have a use case that needs to make calls to a client, defined as an interface, a port.

I want to use one implementation (adapter) or another, for that port; and the choice of which one to use depends on business logic - say, the country of the user, or a more complex calculation.

In terms of design patterns, that sort of smells like a factory to me; I could just have a function typed to return the interface and whose logic returns different implementations based on certain conditions.

However, I'm having trouble integrating this with my knowledge of architecture, and with what is and isn't domain:

  • If I create a domain function that chooses between one adapter or another, I need to have the adapters (non domain code) imported into my domain, so I can return the proper one. That's wrong, because I'm letting implementations be part of business logic.

  • On the other hand I could have an "adapter of adapters": I "leak" through the port the data needed to choose an adapter as part of the contract, and I have one single "wrapper adapter", that I always use for that port, which redirects the call to one of any possible third adapters. This is also wrong, since I have business logic (how to choose one adapter or another) outside the domain, and adapters calling other adapters (?).

How can I solve this problem? Is there a third option I'm not seeing?

kace91
  • 821
  • 1
  • 10
  • 23
  • Do you need to query for the respective implementation of the port inside the domain layer? Or can you make that decision upfront in the application layer? – Andreas Hütter Aug 11 '22 at 18:42
  • @afh let’s say the decision of which implementation to use is complex enough that the choice itself can be considered domain logic (requires retrieving data from other clients or something similar) – kace91 Aug 11 '22 at 21:56

1 Answers1

0

If the interface (business logic) the adapters implement, provides the information needed by the factory to decide which implementation to use then the factory can be business logic as well.

In your example the interface could provide an API providing the country the implementation can be used for.

plainionist
  • 2,950
  • 1
  • 15
  • 27