适用场景
构建需要长期可维护性和可测试性的新功能。 重构分层或框架密集型代码,其中领域逻辑与I/O关注点混杂。 为同一用例支持多种接口(HTTP、CLI、队列工作器、定时任务)。 替换基础设施(数据库、外部API、消息总线)而无需重写业务规则。
affaan-m/ECC
Review hexagonal-architecture's use cases, installation, workflow, and original source instructions.
npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/hexagonal-architecture"Source checked Jul 28, 2026·Refresh due Oct 26, 2026
Reorganized from the pinned upstream SKILL.md
六边形架构(端口与适配器)使业务逻辑独立于框架、传输层和持久化细节。核心应用依赖于抽象端口,而适配器在边缘实现这些端口。
npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/hexagonal-architecture"The pinned source supports a structured brief, but not an expanded tutorial. Only detected inputs, outputs, and sections are shown.
370 source words · 22 usable sections
Documentation workflow
Sections are extracted automatically from the pinned SKILL.md and link back to the source.
构建需要长期可维护性和可测试性的新功能。 重构分层或框架密集型代码,其中领域逻辑与I/O关注点混杂。 为同一用例支持多种接口(HTTP、CLI、队列工作器、定时任务)。 替换基础设施(数据库、外部API、消息总线)而无需重写业务规则。
领域模型:业务规则和实体/值对象。无框架导入。 用例(应用层):编排领域行为和工作流步骤。 入站端口:描述应用能力的契约(命令/查询/用例接口)。 出站端口:应用所需依赖的契约(仓库、网关、事件发布器、时钟、UUID等)。 适配器:端口的基础设施和交付实现(HTTP控制器、数据库仓库、队列消费者、SDK封装器)。 组合根:将具体适配器绑定到用例的单一连接位置。
定义具有清晰输入和输出DTO的单个用例。将传输细节(Express req、GraphQL context、任务负载包装器)保持在此边界之外。
定义具有清晰输入和输出DTO的单个用例。将传输细节(Express req、GraphQL context、任务负载包装器)保持在此边界之外。
Documentation checklist
The source section “适用场景” has been checked.
The source section “核心概念” has been checked.
The source section “工作原理” has been checked.
The source section “步骤1:建模用例边界” has been checked.
Choose a different workflow
FAQ
六边形架构(端口与适配器)使业务逻辑独立于框架、传输层和持久化细节。核心应用依赖于抽象端口,而适配器在边缘实现这些端口。
The source record exposes this install command: npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/hexagonal-architecture". Inspect the command and pinned source before running it.
Quality breakdown
Based on traceable docs and repository signals; stars are not treated as quality.
Compare before choosing
These links are selected from shared tasks, functions, stacks, platforms, and same-name variants. Compare the source owner, documentation, permissions, and maintenance signals.
六边形架构(端口与适配器)使业务逻辑独立于框架、传输层和持久化细节。核心应用依赖于抽象端口,而适配器在边缘实现这些端口。
当需求涉及边界、领域驱动设计、重构紧耦合服务,或将应用逻辑与特定库解耦时,使用此技能。
出站端口接口通常位于应用层(仅当抽象真正属于领域层时才位于领域层),而基础设施适配器实现它们。
依赖方向始终向内:
定义具有清晰输入和输出DTO的单个用例。将传输细节(Express req、GraphQL context、任务负载包装器)保持在此边界之外。
将每个副作用识别为端口:
UserRepositoryPort)BillingGatewayPort)LoggerPort、ClockPort)端口应建模能力,而非技术。
用例类/函数通过构造函数/参数接收端口。它验证应用层不变量,协调领域规则,并返回纯数据结构。
实例化适配器,然后将其注入用例。保持此连接集中化,以避免隐藏的服务定位器行为。
flowchart LR
Client["Client (HTTP/CLI/Worker)"] --> InboundAdapter["Inbound Adapter"]
InboundAdapter -->|"calls"| UseCase["UseCase (Application Layer)"]
UseCase -->|"uses"| OutboundPort["OutboundPort (Interface)"]
OutboundAdapter["Outbound Adapter"] -->|"implements"| OutboundPort
OutboundAdapter --> ExternalSystem["DB/API/Queue"]
UseCase --> DomainModel["DomainModel"]
使用以功能为先的组织方式,并带有显式边界:
src/
features/
orders/
domain/
Order.ts
OrderPolicy.ts
application/
ports/
inbound/
CreateOrder.ts
outbound/
OrderRepositoryPort.ts
PaymentGatewayPort.ts
use-cases/
CreateOrderUseCase.ts
adapters/
inbound/
http/
createOrderRoute.ts
outbound/
postgres/
PostgresOrderRepository.ts
stripe/
StripePaymentGateway.ts
composition/
ordersContainer.ts
export interface OrderRepositoryPort {
save(order: Order): Promise<void>;
findById(orderId: string): Promise<Order | null>;
}
export interface PaymentGatewayPort {
authorize(input: { orderId: string; amountCents: number }): Promise<{ authorizationId: string }>;
}
type CreateOrderInput = {
orderId: string;
amountCents: number;
};
type CreateOrderOutput = {
orderId: string;
authorizationId: string;
};
export class CreateOrderUseCase {
constructor(
private readonly orderRepository: OrderRepositoryPort,
private readonly paymentGateway: PaymentGatewayPort
) {}
async execute(input: CreateOrderInput): Promise<CreateOrderOutput> {
const order = Order.create({ id: input.orderId, amountCents: input.amountCents });
const auth = await this.paymentGateway.authorize({
orderId: order.id,
amountCents: order.amountCents,
});
// markAuthorized returns a new Order instance; it does not mutate in place.
const authorizedOrder = order.markAuthorized(auth.authorizationId);
await this.orderRepository.save(authorizedOrder);
return {
orderId: order.id,
authorizationId: auth.authorizationId,
};
}
}
export class PostgresOrderRepository implements OrderRepositoryPort {
constructor(private readonly db: SqlClient) {}
async save(order: Order): Promise<void> {
await this.db.query(
"insert into orders (id, amount_cents, status, authorization_id) values ($1, $2, $3, $4)",
[order.id, order.amountCents, order.status, order.authorizationId]
);
}
async findById(orderId: string): Promise<Order | null> {
const row = await this.db.oneOrNone("select * from orders where id = $1", [orderId]);
return row ? Order.rehydrate(row) : null;
}
}
export const buildCreateOrderUseCase = (deps: { db: SqlClient; stripe: StripeClient }) => {
const orderRepository = new PostgresOrderRepository(deps.db);
const paymentGateway = new StripePaymentGateway(deps.stripe);
return new CreateOrderUseCase(orderRepository, paymentGateway);
};
在不同生态系统中使用相同的边界规则;仅语法和连接方式发生变化。
application/ports/* 作为接口/类型。adapters/inbound/*、adapters/outbound/*。domain、application.port.in、application.port.out、application.usecase、adapter.in、adapter.out。application.port.* 中的接口。@Service 是可选的,非必需)。domain、application.port、application.usecase、adapter)。internal/<feature>/domain、application、ports、adapters/inbound、adapters/outbound。New... 构造函数的结构体。cmd/<app>/main.go 中连接(或专用连接包),保持构造函数显式。req、res 或队列元数据读取。