3

Flow of Hexagonal Architecture

I have read different sources about Ports & Adapters architecture proposed by Alistair Cockburn and find it apt for my scenario of developing a gateway service application which receives message from multiple sources and process the message and send the message to multiple destinations. Here is my implementation in detail.

  • Currently the source of message is single(JMS Queue)
  • The JMS Port subscribes to the JMS Message Queue and passes it to JMS Adapter which in turn invokes the corresponding message handler.
  • The Message handler in turn calls the business domain layer which is independent of the source or destination of message as proposed by cockburn.
  • The Message handler which has JMS Port,WCF Port,DB Port,TCP Port injected by dependency injection container in turn invokes the JMS Port,TCP Port and WCF Port to publish/send the domain processed message.

I have several key questions/doubts if whether i have deviated from Cockburn's proposed architecture.

  1. Can a single Port handle both inflow/outflow of message(In this case JMS Port). Or is it a good practice to have seperate ports for inflow and outflow of messages.

2.As per the cockburn's article it says

Inbound communication: "As events arrive from the outside world at a port, a technology-specific adapter converts it into a usable procedure call or message and passes it to the application."

Outbound communication: "When the application has something to send out, it sends it out through a port to an adapter, which creates the appropriate signals needed by the receiving technology (human or automated)."

So i have passed on the processed message directly to the port which in turn calls the adapter to transform the message as per the destination requirements.

  1. Can Message Handlers (Application Layer) have the dependency of Ports injected ?

  2. As per the article by Cockburn it says

There will typically be multiple adapters for any one port, for various technologies that may plug into that port.

I can't think of a scenario where mulitple adapters are required for a single port. Can you throw me a scenario so that i can leverage the architecture to the fullest.

Dilip Nandakumar
  • 198
  • 2
  • 14

1 Answers1

2

Here is my point of view:

  1. Yes, a port can have both inflow and outflow operations. To me, it depends by the use-cases that the hexagon implements and the reasons why it uses that port. e.g. I can think of a port "IPersistenceGateway" that has methods for read and write like "GetUsers" and "SaveUser". That port represents the abstraction of persistence for all the hexagon; in other cases I could have "IReadingPersistenceGateway" and "IWritingPersistenceGateway" where the two operations mentioned above are splitted to, in order to separate reading/writing operations "à la CQRS".

  2. I think that your "Application layers" should be considered "inside the hexagon": so yes, message handlers can have dependency of ports injected. I think that message handlers should be the only objects that are visible from the outside of the hexagon and therefore the first objects that obtain the ports, injected by the outside.

  3. In the example I mentioned above, where the port is "IPersistenceGateway", there could be a "MySqlPersisteceGateway" and a "MongoPersistenceGateway" that manage the asbtracted persistence needs of the hexagon, for MySql and MongoDb context.

In your case, for what I understand, I think that the JMS adapter could have injected the message handler of the hexagon because the JMS adapter has to "push" messages to the hexagon and not vice-versa (the xexagon that pulls messages form an adapter). If this is correct, there is no problem (in my opinion) to have an adapter that references directly a message handler inside the hexagon. The point is always the direction of dependencies: always toward the abstraction, from the outside the hexagon to the inside (DIP, Dependency Inversion Principle).

Paolo Laurenti
  • 2,364
  • 2
  • 12
  • 18