0

I'm trying to use the hexagonal architecture but I want the flexibility to make CUSTOM queries right to the database (or cache) to populate DTOs right from the left-side without passing through the domain layer. I think this is something related to CQRS, right?

The question is: Is that ok to make the left-side layer knows the right-side? Isn't that a violation of hexagonal architecture principles? Are there other options?

The layers are three mavens modules (app as left-side, domain, and infra as right-side) sharing a parent pom in my project.

Thank you in advance.

2 Answers 2

2

Why do you want the app to make a custom query that the domain doesn't know about? That sounds like you are putting domain knowledge in the app layer where it shouldn't belong. CQRS is a pattern concerned with how an infrastructure/persistence layer is structured, the domain doesn't technically have to know about CQRS. You don't want the app talking to the infrastructure directly, because that opens the door to the domain not holding any logic and everything being coupled in the app/infrastructure projects.

1
  • Thank you for the answer. I just need to know if my data (loaded from the database) always needs to be translated to domain objects (domain layer) before being presented by the application layer.
    – CelinHC
    Sep 20, 2021 at 10:34
1

Here's how I see your question. Given the standard Hexigon (drawn here as a rectangle):

            +-Hexagon--------------------------------+
            |                                        |
+---------+ |  +-------+    +-------+   +--------+   | +---------+
|  Input  +-+->| Input |<|--+ Logic +-->| Output |<|-+-+ Output  |
| Adapter | |  | Port  |    +-------+   |  Port  |   | | Adapter |
+---------+ |  +-------+                +--------+   | +---------+
            |                                        |
            +----------------------------------------+
  • InputAdaptor sends messages to InputPort
  • Logic sends messages to OutputPort
  • Logic implements the InputPort interface
  • OutputAdapter implements the OutputPort interface

You seem to be asking if an InputAdapter can send messages directly to the OutputAdapter... If you do that, then it will be impossible to test either in isolation. This would break the architecture.

"But my use-case has no logic!" you might say. Maybe tapping a button in the UI should directly cause an output effect. I'd say you are mistaken if you think this. At minimum, there is logic to determine which output effect should be activated as a result of that input event.

The only way you could do this and still maintain the architecture is if you configured your InputAdaptor to send messages directly to an OutputPort rather than an InputPort, thus bypassing the Logic. This is certainly possible to do, but it might be confusing.

2
  • Thank you for the answer. I just need to know if my data (loaded from the database) always needs to be translated to domain objects (domain layer) before being presented by the application layer.
    – CelinHC
    Sep 20, 2021 at 10:33
  • As long as the input adaptor doesn't know that the database exists, and doesn't need to be modified if the database is changed or replaced, and replacing the input doesn't require you to make changes to the DB you are good.
    – Daniel T.
    Sep 20, 2021 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.