Reputation: 143
I'm reading a lot about hexagonal architecture, but in all the examples that i'm looking, all folders and class ubication are different, and this looks a bit confusing to me.
I've done a simple spring boot application with below folder structure. Adapter folder contains implementation of repository interface and the rest controller.
In domain folder, I've model, which is a simple POJO, ports, that are interfaces of service class which contains all the business logic of Product, and the interface of repository which exposes the methods to be implemented in repository.
In another folder I've service implementation, and as i told before, with the business logic of product.
Is this a correct way to implement an hexagonal architecture for a simple use case? If not, why? Where i should put every class and why? This is what it's not clear...
Much appreciate!
Upvotes: 11
Views: 14043
Reputation: 2424
You are totally free to organise your code however you wish. This is unrelated to hexagonal architecture.
With that being said, if you want to use hexagonal architecture efficiently, you should probably follow a domain-driven design, that is to say, you should organise your code based on domain/business logic, and not based on technical similarities.
For example, instead of having the following structure:
controller
product
cart
customer
service
product
cart
customer
repository
product
cart
customer
DDD recommends the following structure:
product
controller
service
repository
cart
controller
service
repository
customer
controller
service
repository
Once you've done this, if it helps, you could wrap these in three packages for the differents parts of the hexagonal architecture: user side, business logic and server side. This is something I've done in the past; it helps me keep the different layers clear.
userside
product
controller
cart
controller
customer
controller
businesslogic
product
service
cart
service
customer
service
serverside
product
service
cart
repository
customer
repository
Again, the structure of your packages is not the most important concept. The hexagonal architecture focuses on three principles:
It's a very DDD oriented architecture; the idea is that the business logic remain as close to the domain as possible, and unadulterated by technical requirements.
Upvotes: 12
Reputation: 4754
Hexagonal architecture doesn't say snything about how to organize the hexagon code (business logic). You can implement however you want to.
Take a look at these articles:
https://jmgarridopaz.github.io/content/articles.html
Upvotes: 3