Senior
Are you familiar with Hexagonal Architecture? Tell about its structure.
sobes.tech AI
Answer from AI
Hexagonal Architecture, or Ports and Adapters, is an architectural style that separates the application into a core and external interfaces. The core contains business logic (domain) that does not depend on external systems such as databases, UI, or external services.
The structure includes:
- Domain (core) — contains business rules and logic.
- Ports — interfaces through which the core interacts with the outside world (e.g., repositories, services).
- Adapters — implementations of ports for specific technologies (DB, web, UI).
This allows easy replacement of external components without changing business logic, improving testability and maintainability.
Example in Go:
// Port
type UserRepository interface {
GetUser(id string) (*User, error)
}
// Domain
func GetUserName(repo UserRepository, id string) (string, error) {
user, err := repo.GetUser(id)
if err != nil {
return "", err
}
return user.Name, nil
}
// Adapter
type InMemoryUserRepo struct {
users map[string]*User
}
func (r *InMemoryUserRepo) GetUser(id string) (*User, error) {
user, ok := r.users[id]
if !ok {
return nil, errors.New("user not found")
}
return user, nil
}
This approach isolates business logic from implementation details.