# 六边形架构 (hexagonal-architecture)

> 按端口与适配器模式设计、实现或重构系统，让业务逻辑独立于框架、传输和持久化细节。

- 分类: [架构设计](https://www.myskills.tools/zh/categories/architecture/)
- 来源: 社区 · Affaan Mustafa
- 代码仓库: https://github.com/affaan-m/ECC/tree/main/skills/hexagonal-architecture (commit d29cf65)
- SKILL.md: https://github.com/affaan-m/ECC/blob/main/skills/hexagonal-architecture/SKILL.md
- 安装命令: `npx skills add affaan-m/ECC --skill hexagonal-architecture`
- 许可证: MIT
- 兼容工具: Claude Code, Codex, Cursor, Copilot, OpenCode
- 权限: read-repo ✓, write-files ✓, shell ✗, network ✗
- 状态: 扫描通过
- 规则扫描无法覆盖所有风险，安装前请阅读此技能的 SKILL.md，确认权限后再使用。

## 核心规则

依赖方向永远向内：领域不依赖任何外部事物，适配器在边缘实现端口。

## 执行阶段

1. **划定用例边界** — 为单个用例定义清晰的输入输出 DTO，把 req、GraphQL context 等传输细节挡在边界外。
2. **先定义出站端口** — 把持久化、外部调用、日志和时钟等每个副作用都建模为端口，端口描述能力而非技术。
3. **编排用例** — 用例通过构造函数或参数接收端口，只负责校验不变量、协调领域规则并返回普通数据结构。
4. **适配器与装配** — 在边缘实现入站和出站适配器，映射逻辑留在适配器中，并在唯一的组合根集中装配依赖。
5. **按边界测试** — 用假端口做用例单元测试，用真实基础设施做适配器集成测试；改造既有系统时逐个切片迁移，不做大爆炸式重写。

## 权限说明（据 SKILL.md 整理）

| 能力 | 是否需要 | 说明 |
|---|---|---|
| 文件写入 | 需要 | 创建或重构领域、用例、端口、适配器、组合根和测试代码。 |
| 命令执行 | 不需要 | 技能未要求运行命令。 |
| 网络外发 | 不需要 | 不访问网络。 |

## SKILL.md 头部信息（YAML）

```yaml
---
name: hexagonal-architecture
description: Design, implement, and refactor Ports & Adapters systems with clear domain boundaries, dependency inversion, and testable use-case orchestration across TypeScript, Java, Kotlin, and Go services. Use when introducing or refactoring toward Ports and Adapters, or when domain logic has become entangled with I/O.
metadata:
  origin: ECC
---
```

首页: https://www.myskills.tools/zh/ · URL: https://www.myskills.tools/zh/skills/hexagonal-architecture/
