Business Logic Layer
Responsibility Split
- Handlers SHOULD stay thin.
- Business workflows SHOULD live in services.
- Persistence logic MUST stay in models/repository helpers.
When to Introduce Services
Create/expand service modules when logic includes:
- multiple model operations in one use case
- transaction coordination
- external integrations (cache, email, AI, files)
- domain validation beyond simple input checks
Interface and DI Patterns
- Use interfaces where behavior boundaries improve testing/replacement.
- Keep interfaces minimal and behavior-oriented.
- Prefer constructor injection for services.
- Handlers MUST NOT create DB/Redis/external clients directly.
func NewTokenService(db *gorm.DB) (*TokenService, error) {
if db == nil {
return nil, ErrInvalidDependency
}
return &TokenService{db: db}, nil
}