r/softwarearchitecture • u/Quick-Resident9433 • Apr 19 '26
Discussion/Advice Hexagonal Architecture - Ports
Hi.
I'm learning about Hexagonal Architecture and have some questions about where the in-ports (use case interfaces) and out-ports (repository interfaces) should be placed.
I've read various blogs, articles, and discussions where some people mentioned that ports must be located in the application layer, and others said they must be in the domain layer. I'm confused about the right place to put them.
I'd like to know your opinions and suggestions, please.
Approach 1 - Ports inside application layer
application/
- in/ (use case interfaces)
- CreateProductUseCase
- out/ (repository interfaces)
- ProductRepository
- usecase/
- ProductUseCaseImpl
domain/
- model
- Product (plain object)
Approach 2 - Ports inside domain layer
domain/
- in/ (use case interfaces)
- CreateProductUseCase
- out/ (repository interfaces)
- ProductRepository
- model
- Product (plain object)
application/
- usecase/
- ProductUseCaseImpl
38
Upvotes
15
u/CatolicQuotes Apr 19 '26
First of all hexagonal doesn't have domain and application layer. Those come from onion and clean architecture.
That being said,they go in application layer.
Read this
https://herbertograca.com/2017/07/03/the-software-architecture-chronicles/
5
u/pivovarit Apr 19 '26
Pragmatic take: as long as your core domain doesn't depend on actual integrations, but on these ports, it doesn't matter.
4
u/Tyrulan Apr 19 '26
The port interfaces belong in application layer, which acts as the orchestrator. It can use domain models to execute business logic, and pass those business model to your ports.
Your domain model should remain free of IO behaviour and have limited context of how it’s being used.
Ports are implemented by your infrastructure layer, such as MyCompany.Infrastructure.Persistence or more explicit, MyCompany.Infrastructure.Persistence.EfCore.
Both application and infrastructure layers use the domain.
2
u/gbrennon Apr 19 '26
i think thats a project decision.
u will find ppl that defends that ports belong to:
layers or even that ports are not required.
i only disagree with that project decision that u dont need ports/interfaces.
i like to put ports into the
applicationlayer to keepdomainlayer really about domain business logic.in
applicationlayer will be definitions and impls related toapplicationbusiness logic.application business logicwill be orchestrating blocks/components of ur application.domain business logicwill be about "low level" real business logic so it should represents rules related to rules that ur proejct does require.1
u/flavius-as Apr 19 '26
What hexagonal calls " the application" is in effect the domain model.
Hexagonal does not have many concepts, so getting all of them right is important.
1
u/Substantial_Ad252 Apr 19 '26
i use 3 packages: port, adapter, core per hexagon. port and adapter each have in and out subpackages.
Did i understand something in a very wrong way?
-2
u/Spare-Builder-355 Apr 19 '26 edited Apr 19 '26
ports belong to garbage bin. Same as this whole hexagonal thing. Don't waste your time on it, it will not teach you anything. Or if you insist - you will learn something hexagonal that you never will see used outside of books and blogs.
If you want to learn good architecture start build something with opensource components. More specifically libraries, not huge frameworks that provide "everything out-of-thebox". Standalone libraries are great example of good architecture:
The key criteria to good architecture is testability. Can you test your class/component/library in isolation from the rest of project codebase ? Is answer is yes - congrats, you likely have good architecture. Or at least you are on the right way. Do not be too religious either. Some shared code is absolutely necessary, shared types, stateless utils and logging+tracing cover 99% of shared code examples.
Learn about API boundaries. In modern dumb world more often than not API == http endpoint, while there is so much more to it. Once again, best way to learn good api design is to use standalone libraries.
Best code is code that is not written. Learn how to minimize amount of code to write and maintain. Adding some shit like ports and adapters is not that.