From 2d1dd5d4018b94ec962bb62b802e83bdd5d74bc1 Mon Sep 17 00:00:00 2001 From: Victor_Jay Date: Sun, 21 Jun 2026 23:54:48 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=9B=B4=E6=96=B0=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=E8=A7=84=E8=8C=83=20=E2=80=94=20=E6=9E=84?= =?UTF-8?q?=E9=80=A0=E5=99=A8=E6=B3=A8=E5=85=A5=20+=20ISP=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E9=9A=94=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/code-style.md | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/code-style.md b/docs/code-style.md index 5968bcf..11026e3 100644 --- a/docs/code-style.md +++ b/docs/code-style.md @@ -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 +} ```