Hexagonal Architecture Java Example

Overview

Hexagonal Architecture

Dev.to Hexagonal Architecture Article

I have been working on many projects that claim to be using the Hexagonal Architecture Style. All of them inspired this article because they have some interesting ideas or perhaps I didn't agree with the implemented structure (Business logic with Frameworks dependencies).

The goal of this post is to create an open-source artifact that exposes the fundamental ideas of Clean Architecture:

  • Independent of frameworks
  • Testable
  • Independent of UI
  • Independent of database
  • Independent of any external service
  • Inversion of dependencies: Low level (Infrastructure: UI, Database, Integrations) modules depends on High Level abstractions (Business Logic)

Github Repository

To keep the example simple: we create a basic Use-case where based on some resources and the authors/mentors mark them as optional or mandatory.

Java/Kotlin Artifact

Initial considerations:

  • Business logic must be independent (We create a Gradle Module with just plain Java)
  • Framework, database, and those low-level details will be in another module/project (Spring Project in Kotlin)

Low-level abstractions module depends on domain module (Business logic):

implementation(project(":domain"))

Build from source

Compile and run tests: ./gradlew clean build

Run the application: ./gradlew bootRun

Spring boot running

Running HTTP calls:

The HTTPie client in IntelliJ IDEA.

Http test

Clean Architecture

Good design in software is about the way in which we organize the code of the system to manage complexity. The idea is to create modular systems with high cohesion and low coupling, and after all, promote separation of concerns and allow some flexibility to evolve our systems.

"The outer circles are mechanisms. The inner circles are policies".

Business Objects

They don’t have any outward dependency and are the fundamental part of the application. The same language that the business speaks.

Use-Cases

They are the features or descriptions of what the users can do with our system. Actually, what we call the "Business logic".

Input and Output Ports

They are the abstractions of what a user can do (Input ports), and any external integration, database, or low-level detail (Output adapter).

Input and Output Adapters

They are the implementation of the Ports. Low-level details, the database, the HTTP controller, the CLI, basically the glue between the users and the core of our business (use-cases).

Clean Architecture Reflectoring

Onion Architecture

The key tenets of onion architecture

  • The application is built around the business independent model
  • Inner layer define interfaces, outer layer implement interfaces
  • Direction of coupling is toward the center
  • The application core code can be compiled and run separately from Infra code

Design considerations

  • Effort is inversely proportional to Software design
  • Make it work, then make it right
  • The key to going fast is not to build the things that make you go slow
  • First value is to meet customer needs
  • Second value its have a good structure that allows the software to be maintainable
  • Architecture must show intent!
  • Isolate things and defer decisions
  • Acceptance test must be finished in the middle of the sprint

Code Example

  • Module of business logic is independent of the Framework and Database. Just plain Java.
  • Low-Level details were implemented in the Kotlin project.

If we need to change the database, ever the framework is easy. We just need to implement the DomainRepository class with another type of Database, for example, my preferred one: DynamoDB.

Resources

You might also like...
Spring Boot Example with Kotlin and Domain Driven Design

Spring Boot Kotlin Domain Driven Design Example Just an example project where ddd is implemented with some other great patterns and architecture from

A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations.
A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations.

kotlin-ktor-rest-api A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations. F

📌This repo contains the kotlin implementation of TensorflowLite Example Android Apps🚀
📌This repo contains the kotlin implementation of TensorflowLite Example Android Apps🚀

TensorflowLite Examples Kotlin This repo contains the kotlin implementation of TensorflowLite Example Apps here, which are mostly implemented in java

Example project for using the Selenium toolkit with Kotlin, Maven, TestNg and the config is managed via a property file.

Selenium-Java-Toolkit-TestNg-Playground This is the sample-Project and show you how to use the Selenium-Toolkit. The Selenium-Toolkit is a Java based

Spring for Kotlin Example
Spring for Kotlin Example

Spring for Kotlin Example This project is for studying the spring using by kotlin. Required docker-compose Running project docker-compose -f docker-co

grpc stream fullstack example(spring+kotlin / next.js + typescript)

grpc-stream-fullstack chat application build with grpc named qhat Prerequisites server sync .proto files on src/main/proto/grpc/qhat/ $ ./gradlew sync

Android Example project

Example project for the employers. I created this project for the employers because I often get demands about showing some of my code. Patterns: MVP C

R2DBC Sharding Example with Kotlin Coroutine

R2DBC Sharding Example with Kotlin Coroutine A minimal sharding example with R2DBC and coroutine, where user table is divided into 2 different shards.

An example of a SQS consumer using Micronaut

Micronaut 3.0.3 Documentation User Guide API Reference Configuration Reference Micronaut Guides Feature http-client documentation Micronaut HTTP Clien

Owner
Software Architect and Developer!
null
Account-hexa-service-kotlin - Microservice with Kotlin using Hexagonal architecture

Microservice Construindo microservice de conta para fins Didáticos. Objetivos Cr

Adriano Marcião 1 Jan 30, 2022
Aplicação Micronaut GRPC utilizando Kotlin e arquitetura Hexagonal

micronaut-grpc-demo Aplicação Micronaut GRPC utilizando Kotlin e arquitetura Hexagonal #Baixando e configurando um container Postgres: Postgres: docke

Paulo César de Souza 0 Nov 15, 2021
Exemplo de microsserviço em koltin e spring boot usando a arquitetura hexagonal/ports&adapters como base de estrutura

Microservice boilerplate Exemplo de microsserviço em koltin e spring boot usando a arquitetura hexagonal como base de estrutura Executando Localmente

Odilio 6 Dec 7, 2022
This is an example of a simple application with layered software base on clean-architecture as application architecture and mvvm as presentation architecture

This is an example of a simple application with layered software base on clean-architecture as application architecture and mvvm as presentation archi

null 3 Jul 2, 2021
An example project for configuring Rust with Java 18's Panama/jextract

rust-panama A template project for setting up both Rust and the Java 18 Panama Foreign Linker API (via jextract). How to Setup Clone the repo. Modify

Aidan 14 Jul 5, 2022
A clean architecture example. Using Kotlin Flow, Retrofit and Dagger Hilt, etc.

android-clean-architecture A clean architecture example. Using Kotlin Flow, Retrofit and Dagger Hilt, etc. Intro Architecture means the overall design

Prieyudha Akadita S. 69 Dec 21, 2022
FunFacts is an example of Modular architecture

FunFacts FunFacts is an example of Modular architecture This Project is built using Kotlin,we will use in this App : Modular architecture Dependency I

Sachin Rajput 48 Jan 1, 2023
Learning Project (Story App) For Applying Android Architecture Components And Clean Architecture Using MVVM With Kotlin

Learning Project (Story App) For Applying Android Architecture Components And Clean Architecture Using MVVM With Kotlin. Implemented by Clean Architecture, Hilt, MVVM, LiveData, Coroutines, Retrofit2, Glide

Samad Talukder 4 Sep 27, 2022
Example of Android project showing integration with Kotlin and Dagger 2

kotlin-dagger-example This project demonstrate how to setup an Android Project with Kotlin and Dagger 2. It's based on Dagger 2 example ##Known issues

Damian Petla 533 Nov 11, 2022
🎓 Learning Kotlin Coroutines for Android by example. 🚀 Sample implementations for real-world Android use cases. 🛠 Unit tests included!

Kotlin Coroutines - Use Cases on Android ?? Learning Kotlin Coroutines for Android by example. ?? Sample implementations for real-world Android use ca

Lukas Lechner 2.1k Jan 3, 2023