AgentSkillsCN

vertical-slice-architecture

运用 Clean Architecture 分层架构模式,打造垂直切片架构。在规划功能、创建切片、实现处理器,或审查 VSA 代码结构时,均可使用此方法。触发条件包括:VSA、垂直切片、功能切片、处理器、命令、查询、MediatR、切片结构。

SKILL.md
--- frontmatter
name: vertical-slice-architecture
description: "Vertical Slice Architecture patterns with Clean Architecture layers. Use when planning features, creating slices, implementing handlers, or reviewing VSA code structure. Triggers on: VSA, vertical slice, feature slice, handler, command, query, MediatR, slice structure."

Vertical Slice Architecture

Core Principles

Organise by feature, not layer. Code that changes together lives together. A slice contains everything for a single use case.

Maintain Clean Architecture boundaries. Domain and infrastructure remain in their own layers. Slices depend inward on domain.

Isolate slices from each other. No direct slice-to-slice references. Use domain events or go through domain layer.

Keep transport layers thin. Endpoints initialise command/query, dispatch, transform result. No business logic.

Project Structure

code
src/
├── Features/                        # Vertical slices by feature area
│   ├── Rates/
│   │   ├── CreateRate/
│   │   │   ├── CreateRateCommand.cs
│   │   │   ├── CreateRateHandler.cs
│   │   │   ├── CreateRateValidator.cs
│   │   │   └── CreateRateEndpoint.cs
│   │   └── GetRate/
│   │       ├── GetRateQuery.cs
│   │       ├── GetRateHandler.cs
│   │       ├── GetRateResponse.cs
│   │       └── GetRateEndpoint.cs
├── Domain/                          # Entities, value objects, domain services
└── Infrastructure/                  # Persistence, external services

Quick Reference

ComponentPurposeLocation
Command/QueryRequest objectFeatures/{Area}/{Operation}/
HandlerBusiness logicSame slice folder
ValidatorStructural validationSame slice folder
ResponseQuery DTOSame slice folder (not shared)
EndpointHTTP thin layerSame slice folder
Read StoreQuery projectionsInfrastructure/ReadStores/

Key Conventions

  • Commands: ICommand or ICommand<T>; queries: IQuery<T>
  • All handlers return Result<T> (FluentResults)
  • Inject ISender, not IMediator
  • Response DTOs co-located and not shared across slices
  • Validators: structural only; business rules in handlers

Detailed References

Slice Components: See reference/slice-components.md for full code examples.

Code Review Checklist: See reference/code-review-checklist.md for VSA-specific review criteria.

When to Create a New Slice

Create when adding a new use case with distinct input/output.

Don't create when adding minor variation (extend existing) or shared utilities (use appropriate layer).

Cross-Slice Communication

Never import handlers, commands, or responses from other slices. Instead: domain layer, domain events, MediatR notifications, or direct database query (read-only).