00 - Types of Layered Architectures
Layered Architecture
Client
β
βΌ
Controller
β
βΌ
Service
β
βΌ
Repository
β
βΌ
Database
Responsibilities
- Controller β Receives HTTP requests and returns HTTP responses.
- Service β Contains the applicationβs business logic.
- Repository β Handles communication with the database.
- Entity β Represents a database table (JPA).
- DTO (Data Transfer Object) β Object used for API requests and responses.
- Config β Spring configuration classes.
- Utility β Reusable helper functions.
Feature-based Architecture (Package by Feature)
user/
βββ UserController
βββ UserService
βββ UserRepository
βββ UserDTO
βββ User
product/
βββ ProductController
βββ ProductService
βββ ProductRepository
βββ ProductDTO
βββ Product
Each feature contains all the classes related to a specific business capability.
Hexagonal Architecture (Ports & Adapters)
Commonly used in large enterprise applications, fintechs, and banks.
Controller
β
βΌ
Application Service
β
βΌ
Domain
β²
β
Repository Interface
β²
β
JPA Repository
The domain layer does not depend on Spring, HTTP, or the database.
Advantages
- Easy to test
- Loosely coupled
- Easy to replace the database
- Easy to replace REST with Kafka, GraphQL, or gRPC
Clean Architecture
An evolution of Hexagonal Architecture.
Controller
β
βΌ
Use Cases
β
βΌ
Domain
β
βΌ
Infrastructure