docs: 更新依赖注入规范 — 构造器注入 + ISP 接口隔离
This commit is contained in:
@ -100,12 +100,23 @@ gin.H{
|
||||
|
||||
## 依赖注入
|
||||
|
||||
当前阶段 Controller 直接在方法内创建 Service 实例。后期引入 DI 容器后统一管理。
|
||||
使用构造器注入模式,Controller/Service/Middleware 的依赖通过构造函数传入:
|
||||
|
||||
```go
|
||||
// 当前
|
||||
func (ac *AuthController) RegisterPage(c *gin.Context) { ... }
|
||||
|
||||
// 后期
|
||||
func NewAuthController(svc *AuthService) *AuthController { ... }
|
||||
// 构造器注入依赖
|
||||
func NewAuthController(authService authUseCase, sm *session.Manager, limiter rateLimiter, cfg *config.Config, siteSettings *config.SiteSettings) *AuthController {
|
||||
return &AuthController{authService: authService, sessionManager: sm, rateLimiter: limiter, cfg: cfg, siteSettings: siteSettings}
|
||||
}
|
||||
```
|
||||
|
||||
Controller 层通过 **ISP 接口隔离** 声明最小依赖 —— 只声明自己需要的方法子集,不依赖完整 Service 接口:
|
||||
|
||||
```go
|
||||
// controller/admin/interfaces.go
|
||||
type siteSettingUseCase interface {
|
||||
GetSettings() map[string]string
|
||||
UpdateSetting(key, value string) error
|
||||
UpdateBoolSetting(key string, value bool) error
|
||||
GetAuditSettings(defaults *service.AuditDefaults) *service.AuditSettings
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user