Skip to content
Back to skills

Clean Architecture

ASecurity

Structure codebases with clean architecture: layers, dependency rule, use cases, ports and adapters. Use for maintainable large codebases.

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 1, 2026
ai-agentspythongobashsql

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned October 1, 2026

npx -y skills add ssrjkk/agent-skills --skill clean-architecture --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Clean Architecture?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Clean Architecture
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ssrjkk-clean-architecture/badge)](https://www.skillsdirectory.com/skills/ssrjkk-clean-architecture)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: clean-architecture
description: "Structure codebases with clean architecture: layers, dependency rule, use cases, ports and adapters. Use for maintainable large codebases."
category: engineering
tags: [clean-architecture, architecture, hexagonal, dependency-rule, use-cases]
models: [sonnet, opus, gpt-6, gemini-3, glm-5]
version: 1.0.0
created: 2026-09-29
updated: 2026-09-29
author: ssrjkk
---
# Clean Architecture

> Organizing codebases for maintainability and testability.

## Quick Start
```text
Layers (outer -> inner):
  Frameworks/Drivers -> Interface -> Application -> Domain
Dependency rule: dependencies point inward only.
```

## When to Use
- Large, long-lived codebases
- Complex business domains
- Teams needing testability
- Systems with changing infrastructure

## Best Practices

### Dependency Rule
- Dependencies point inward only
- Domain has no framework imports
- Outer layers depend on interfaces
- Invert dependencies with DI

### Layers
- Domain: entities and business rules
- Application: use cases and ports
- Interface: controllers/presenters
- Infrastructure: DB, HTTP, external

### Use Cases
- Model each business action as a use case
- Keep use cases thin and focused
- Ports define interfaces to the outside
- Adapters implement ports

### Boundaries
- Cross boundaries with interfaces
- Map data at the boundary (DTOs)
- Keep frameworks at the edges
- Test each layer in isolation

## Dependencies
```bash
# language-agnostic; DI container helps
```

## Examples
```python
# Domain entity (no framework imports)
class Order:
    def __init__(self, items: list[float]):
        if any(i <= 0 for i in items):
            raise ValueError("invalid item")
        self.items = items

    def total(self) -> float:
        return sum(self.items)
```
```python
# Port (interface to the outside)
from abc import ABC, abstractmethod

class OrderRepository(ABC):
    @abstractmethod
    def save(self, order: Order) -> None: ...

    @abstractmethod
    def find(self, order_id: int) -> Order | None: ...
```
```python
# Use case in the application layer
class CreateOrder:
    def __init__(self, repo: OrderRepository):
        self._repo = repo

    def execute(self, items: list[float]) -> Order:
        order = Order(items)
        self._repo.save(order)
        return order
```
```python
# Adapter implementing the port
class SqlOrderRepository(OrderRepository):
    def save(self, order: Order) -> None:
        db.insert("orders", {"items": order.items})
```

## Step-by-Step
1. Define the domain entities and business rules first.
2. Identify use cases (actions) the app must support.
3. Define ports (interfaces) the domain needs.
4. Implement adapters (DB, HTTP) outside the domain.
5. Wire everything with dependency injection.
6. Keep frameworks at the edges.
7. Test domain and use cases without infra.
8. Keep the dependency rule enforced.

## Validation
1. Domain has no framework imports
2. Dependencies point inward
3. Use cases are testable without infra
4. Changing a DB/HTTP lib doesn't ripple through the core
5. Each layer is independently testable

## Troubleshooting
- Framework creep: move framework code to adapters.
- Anemic domain: put business rules in entities.
- Leaky boundaries: map data at the boundaries with DTOs.

Files in this skill

  • SKILL.md3.2 KB
  • SKILL.ru.md4.6 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…