Back to Skills

backend-patterns

verified

Backend development patterns for services, error handling, logging, caching. Use when building backend services, APIs, or microservices.

View on GitHub

Marketplace

claude-code-best-practices

xiaobei930/claude-code-best-practices

Plugin

cc-best

Repository

xiaobei930/claude-code-best-practices
1stars

skills/backend-patterns/SKILL.md

Last Verified

January 25, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/xiaobei930/claude-code-best-practices/blob/main/skills/backend-patterns/SKILL.md -a claude-code --skill backend-patterns

Installation paths:

Claude
.claude/skills/backend-patterns/
Powered by add-skill CLI

Instructions

# 后端开发模式

本技能提供后端服务开发的最佳实践和模式,支持多语言按需加载。

## 触发条件

- 构建后端服务
- 设计服务架构
- 实现业务逻辑层
- 配置中间件
- 处理错误和日志

## 语言专属模式

根据项目技术栈,加载对应的语言专属文件:

| 技术栈             | 加载文件        | 框架                     |
| ------------------ | --------------- | ------------------------ |
| Python             | `python.md`     | FastAPI, Django, Flask   |
| TypeScript/Node.js | `typescript.md` | Express, NestJS, Fastify |
| Java               | `java.md`       | Spring Boot, Quarkus     |
| Go                 | `go.md`         | Gin, Echo, Fiber         |
| C#                 | `csharp.md`     | ASP.NET Core             |

**加载方式**: 检测项目中的 `pyproject.toml`/`package.json`/`pom.xml`/`go.mod` 等文件确定技术栈。

---

## 通用架构模式

### 分层架构

```
┌─────────────────────────────────────┐
│           Controller 层              │  处理 HTTP 请求/响应
├─────────────────────────────────────┤
│            Service 层                │  业务逻辑
├─────────────────────────────────────┤
│          Repository 层               │  数据访问
├─────────────────────────────────────┤
│             Model 层                 │  数据模型
└─────────────────────────────────────┘
```

### 通用目录结构

```
src/
├── controllers/          # 控制器(或 routes/handlers)
├── services/             # 业务逻辑
├── repositories/         # 数据访问(或 dal/)
├── models/               # 数据模型(或 entities/)
├── middlewares/          # 中间件
├── utils/                # 工具函数
├── config/               # 配置
└── types/                # 类型定义(如适用)
```

---

## 通用最佳实践

### 1. 错误处理原则

```
┌─────────────────────────────────────────────────────┐
│                    错误处理金字塔                     │
├─────────────────────────────────────────────────────┤
│  业务错误 (400-499)                                  │
│  ├─ ValidationError (400)   输入验证失败             │
│  ├─ UnauthorizedError (401) 未认证                  │
│  ├─ ForbiddenError (403)    无权限                  │
│  └─ NotFoundError (404)     资源不存在              │
├─────────────────────────────────────────────────────┤
│  系统错误 (500-599)                               

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
3365 chars