Ports & Adapters architecture on example

Photo by Bonniebel B on Unsplash

Introduction

Day 1. Where are controllers, services and repositories packages?

Day 2. Application, core & infrastructure

  • applicationdefines how the outside world interact with an application, it is a gateway to an application core. It might be by Rest controllers, but it could be also by some kind of a message service (like Kafka, RabbitMQ, etc.), command line client or else,
  • core — here sits the business logic of your application. The goal is to have it written in a plain language so that an analyst or even non-technical person could understand. Inside of it we use a domain specific language, which can be easily understand by business people. And another thing, it should be agnostic of any Java framework (like Spring, Jakarta EE, Quarkus, Micronaut), because these are only scaffolding of an application. And the core is the heart of an application, something that encapsulates the logic.
  • infrastructure — it’s the last part, most of the applications does not only contain business logic but usually they also need to use some external systems, like database, queue, sFTP server, other application and so on. In this part we say how this communication will will be implemented (in a core we only say what is needed). For example, in order to persist data in a database we can use several approaches like Hibernate, plain Jdbc, jOOQ or whatever framework we like.

Day 3. Implementing business core

Day 4. Database adapter and dependency injection

Conclusion

  • at least part of a code (business core) can be understandable by non-programmers (business analysts, product owners, your parents, etc.),
  • the core code is decoupled from the infrastructure which makes very easy to replace the adapters without changing the business core code (I found this very useful especially in microservice world, when your app depends on several other APIs which are constantly changing their versions),
  • the core is agnostic of an application framework, the old one can be replaced to Spring Boot, Jakarta EE, Quarkus, Micronaut or whatever other framework is popular at the moment,
  • writing unit tests for the core is very simple and fast, we don’t need to create framework-specific test set up (e.g. in Spring, we don’t need to add @SpringBootTest annotation and build the entire Spring context just to test small part of an application), simple Java will be enough.

References

--

--

--

Java Software Developer, DevOps newbie, constant learner, podcast enthusiast.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Meet Samiha, one of our #ReCodedFellows.

Subscribing to Google PubSub Topics with Spring Boot GCP

Delegates in C#… why we need that?

Soar your Bet in Data Science Using Unix Cmds

Barrier On OpenSUSE and Windows 10

Top 5 Kotlin features we’re missing in Java

Kubernetes: Evenly Distribution of Pods Across Cluster Nodes

Touchless Multifunctional Bedside Lamp With Digital Clock

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store