clean-architecture

Clean Architecture layered design. Use for maintainable code.

7 stars

Best use case

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

Clean Architecture layered design. Use for maintainable code.

Teams using clean-architecture 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/clean-architecture/SKILL.md --create-dirs "https://raw.githubusercontent.com/G1Joshi/Agent-Skills/main/skills/architecture/clean-architecture/SKILL.md"

Manual Installation

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

How clean-architecture Compares

Feature / Agentclean-architectureStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Clean Architecture layered design. Use for maintainable code.

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

# Clean Architecture

Clean Architecture, popularized by Robert C. Martin (Uncle Bob), separates software into layers to ensure independence from frameworks, databases, and UIs. The core principle is the **Dependency Rule**: source code dependencies can only point inwards.

## When to Use

- Building enterprise applications with complex business logic.
- Long-lived projects where frameworks/databases might change over time.
- Large teams requiring clear separation of concerns to work in parallel.

## Quick Start

```typescript
// 1. Entity (Enterprise Logic) - Inner Layer
class User {
  constructor(
    public id: string,
    public name: string,
  ) {
    if (name.length < 2) throw new Error("Name too short");
  }
}

// 2. Use Case (Application Logic)
class CreateUserUseCase {
  constructor(private userRepository: UserRepository) {} // Depends on interface

  async execute(name: string): Promise<User> {
    const user = new User(crypto.randomUUID(), name);
    await this.userRepository.save(user);
    return user;
  }
}

// 3. Interface Adapter (Repository Interface)
interface UserRepository {
  save(user: User): Promise<void>;
}

// 4. Frameworks & Drivers (Implementation) - Outer Layer
class SqlUserRepository implements UserRepository {
  async save(user: User): Promise<void> {
    await db.query("INSERT INTO users ...", [user.id, user.name]);
  }
}
```

## Core Concepts

### The Dependency Rule

Inner layers (Entities) know nothing about outer layers (Controllers, Presenters). Outer layers depend on inner layers.

### Entities

Enterprise-wide business rules. These are the least likely to change when something external changes (e.g., page navigation security).

### Application Business Rules (Use Cases)

Orchestrate the flow of data to and from the entities. They contain the specific business rules of the application (e.g., "Create Order").

## Common Patterns

### Dependency Injection

The glue that makes Clean Architecture possible. Outer layers inject concrete implementations (e.g., `SqlUserRepository`) into inner layers (which expect `UserRepository` interface).

### DTOs (Data Transfer Objects)

Use simple objects (DTOs) to cross boundaries. Do not pass Entities to the UI or Database rows to the Use Case.

## Best Practices

**Do**:

- Define **Interfaces** in the layer that uses them (Interface Segregation).
- Test **Use Cases** in isolation using mocks for repositories.
- Keep **Frameworks** (React, NestJS, Spring) at the outermost layer.

**Don't**:

- Don't let **database entities** (ORM models) leak into the inner layers. Map them to domain Entities.
- Don't skip layers "for speed" (e.g., Controller calling DB directly) in complex apps.

## Troubleshooting

| Error                  | Cause                                   | Solution                                                                        |
| :--------------------- | :-------------------------------------- | :------------------------------------------------------------------------------ |
| `Circular Dependency`  | Violating the dependency rule.          | Use Dependency Inversion (Interfaces) to break the cycle.                       |
| `Boilerplate Overload` | Creating strict layers for simple CRUD. | Consider "Vertical Slice Architecture" or Modular Monolith for simpler domains. |

## References

- [The Clean Architecture Blog](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html)
- [Clean Architecture vs. Hexagonal](https://herbertograca.com/2017/11/16/explicit-architecture-01-ddd-hexagonal-onion-clean-cqrs-how-i-put-it-all-together/)