Skip to content

Commit 895ad3d

Browse files
committed
docs: clarify biz layer supports both Go struct and proto message
- Update architecture diagram to show "protocol-agnostic business logic" - Add "Biz Layer Parameter Types" section documenting both approaches - Update directory structure comment to reference the new section - Sync changes for both EN and ZH documentation
1 parent ca5f98b commit 895ad3d

2 files changed

Lines changed: 76 additions & 4 deletions

File tree

docs/en/advanced/protocol-layer.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ This document describes Bingo's pluggable protocol layer design, supporting any
2323
2424
┌─────────────────────────────────────────────────────────────┐
2525
│ Biz Layer │
26-
(Proto message as params/returns)
26+
(Protocol-agnostic business logic)
2727
└─────────────────────────────────────────────────────────────┘
2828
2929
@@ -47,7 +47,7 @@ internal/apiserver/
4747
├── biz/ # Business logic (protocol-agnostic)
4848
│ ├── biz.go # Interface definitions
4949
│ └── user/
50-
│ └── user.go # Implementation, uses proto messages
50+
│ └── user.go # Implementation (see "Biz Layer Parameter Types" section)
5151
5252
├── handler/ # Protocol handlers (pluggable)
5353
│ ├── http/ # Standalone HTTP (Gin)
@@ -176,6 +176,42 @@ func Run(cfg *config.Config) error {
176176
- **Single Responsibility**: `internal/pkg/server` has no business code dependencies
177177
- **Reusable**: All services (apiserver, admserver, etc.) share the same infrastructure
178178

179+
## Biz Layer Parameter Types
180+
181+
The Biz layer method parameters and return values support two approaches:
182+
183+
### Approach 1: Go Struct (used in this project)
184+
185+
```go
186+
// pkg/api/apiserver/v1/auth.go
187+
type LoginRequest struct {
188+
Username string `json:"username" binding:"required"`
189+
Password string `json:"password" binding:"required"`
190+
}
191+
192+
// internal/apiserver/biz/auth/auth.go
193+
func (b *authBiz) Login(ctx context.Context, req *v1.LoginRequest) (*v1.LoginResponse, error)
194+
```
195+
196+
**Characteristics**:
197+
- Uses `binding` tags for request validation, natively supported by Gin
198+
- Supports native Go types (`time.Time`, `*string` for optional fields, etc.)
199+
- gRPC Handler requires Go Struct ↔ Proto Message conversion
200+
201+
### Approach 2: Proto Message
202+
203+
```go
204+
// Generated from proto file
205+
// internal/apiserver/biz/auth/auth.go
206+
func (b *authBiz) Login(ctx context.Context, req *pb.LoginRequest) (*pb.LoginResponse, error)
207+
```
208+
209+
**Characteristics**:
210+
- Proto file serves as single source of truth for type definitions
211+
- gRPC Handler requires no type conversion
212+
- HTTP Handler requires JSON ↔ Proto conversion
213+
- Validation logic requires protoc-gen-validate or manual implementation
214+
179215
## Handler Implementation
180216

181217
### gRPC Handler

docs/zh/advanced/protocol-layer.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
2424
┌─────────────────────────────────────────────────────────────┐
2525
│ Biz 层 │
26-
(Proto message 作为参数/返回值)
26+
(协议无关的业务逻辑)
2727
└─────────────────────────────────────────────────────────────┘
2828
2929
@@ -47,7 +47,7 @@ internal/apiserver/
4747
├── biz/ # 业务逻辑(协议无关)
4848
│ ├── biz.go # interface 定义
4949
│ └── user/
50-
│ └── user.go # 实现,参数用 proto message
50+
│ └── user.go # 实现(参数类型见「Biz 层参数类型」章节)
5151
5252
├── handler/ # 协议处理器(可插拔)
5353
│ ├── http/ # 独立 HTTP(Gin)
@@ -176,6 +176,42 @@ func Run(cfg *config.Config) error {
176176
- **单一职责**:`internal/pkg/server` 不依赖任何业务代码
177177
- **可复用**:所有服务(apiserver、admserver 等)共用同一套基础设施
178178

179+
## Biz 层参数类型
180+
181+
Biz 层的方法参数和返回值支持两种方案:
182+
183+
### 方案一:Go Struct(本项目示例)
184+
185+
```go
186+
// pkg/api/apiserver/v1/auth.go
187+
type LoginRequest struct {
188+
Username string `json:"username" binding:"required"`
189+
Password string `json:"password" binding:"required"`
190+
}
191+
192+
// internal/apiserver/biz/auth/auth.go
193+
func (b *authBiz) Login(ctx context.Context, req *v1.LoginRequest) (*v1.LoginResponse, error)
194+
```
195+
196+
**特点**
197+
- 使用 `binding` tag 做请求验证,Gin 原生支持
198+
- 支持 Go 原生类型(`time.Time``*string` 可选字段等)
199+
- gRPC Handler 需要做 Go Struct ↔ Proto Message 转换
200+
201+
### 方案二:Proto Message
202+
203+
```go
204+
// 由 proto 文件生成
205+
// internal/apiserver/biz/auth/auth.go
206+
func (b *authBiz) Login(ctx context.Context, req *pb.LoginRequest) (*pb.LoginResponse, error)
207+
```
208+
209+
**特点**:
210+
- Proto 文件作为唯一类型定义
211+
- gRPC Handler 无需类型转换
212+
- HTTP Handler 需要 JSON ↔ Proto 转换
213+
- 验证逻辑需使用 protoc-gen-validate 或手写
214+
179215
## Handler 实现
180216

181217
### gRPC Handler

0 commit comments

Comments
 (0)