hexagonal

Hexagonal architecture ports and adapters. Use for testable systems.

7 stars

Best use case

hexagonal is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Hexagonal architecture ports and adapters. Use for testable systems.

Teams using hexagonal should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/hexagonal/SKILL.md --create-dirs "https://raw.githubusercontent.com/G1Joshi/Agent-Skills/main/skills/architecture/hexagonal/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/hexagonal/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How hexagonal Compares

Feature / AgenthexagonalStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Hexagonal architecture ports and adapters. Use for testable systems.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Hexagonal Architecture (Ports and Adapters)

Hexagonal Architecture aims to create a loosely coupled application component that can easily connect to their software environment by "ports" and "adapters". It treats the database, the web UI, and external APIs as interchangeable "details" (infrastructure).

## When to Use

- When you need to support multiple input channels (e.g., REST API, GraphQL, CLI, Message Queue) for the same business logic.
- When you want to be able to swap "driven" actors (e.g., verify logic with a Mock DB, then swap to Postgres).
- Developing standard microservices.

## Quick Start

```go
// --- CORE (Inside the Hexagon) ---

// Port (Driver Port - Input)
type UserService interface {
    Register(name string) error
}

// Port (Driven Port - Output)
type UserRepository interface {
    Save(user User) error
}

// Application Service (Implementation)
type UserServiceImpl struct {
    repo UserRepository
}

func (s *UserServiceImpl) Register(name string) error {
    return s.repo.Save(User{Name: name})
}

// --- ADAPTERS (Outside the Hexagon) ---

// Driving Adapter (REST API)
func HandleRegister(w http.ResponseWriter, r *http.Request, svc UserService) {
    svc.Register(r.FormValue("name"))
}

// Driven Adapter (Postgres)
type PostgresRepo struct { db *sql.DB }
func (r *PostgresRepo) Save(u User) error { ... }
```

## Core Concepts

### Ports

Interfaces that define the entry and exit points of the application.

- **Driving Ports (In)**: API surfaces (what the app _can do_).
- **Driven Ports (Out)**: Infrastructure dependencies (what the app _needs_).

### Adapters

Concrete implementations that bridge the gap between the Ports and the outside world.

- **Driving Adapters**: REST Controller, gRPC Handler, CLI Command.
- **Driven Adapters**: SQL Repository, SMTP Client, Redis Cache.

## Common Patterns

### Testing with Fake Adapters

Because the core depends on interfaces (Ports), you can implement "Fake" adapters (e.g., `InMemoryRepository`) to test complex business logic without spinning up Docker containers.

## Best Practices

**Do**:

- Keep the **Core** free of any framework dependencies (no HTTP, no SQL, no JSON tags).
- Define **Ports** in the Core, not in the adapters.
- Use **Hexagonal** for the domain layer of a microservice.

**Don't**:

- Don't leak implementation details (like `sql.Rows`) into the Core.
- Don't create an Adapter for every single class; focus on architectural boundaries.

## Troubleshooting

| Error        | Cause                                    | Solution                                                 |
| :----------- | :--------------------------------------- | :------------------------------------------------------- |
| `Leakage`    | Logic depends on specific library types. | Wrap external types in domain-specific DTOs/Interfaces.  |
| `Complexity` | Too many interfaces for simple logic.    | Start with a simple Service/Repository split and evolve. |

## References

- [Alistair Cockburn's Original Paper](https://alistair.cockburn.us/hexagonal-architecture/)
- [Hexagonal Architecture in Go](https://medium.com/@matiasvarela/hexagonal-architecture-in-go-cfd4e436bd57)