📊 Capítulo 09: Clean Architecture em Go
Especialização Backend com Go & Gin Gonic
🗺️ A Separação em Camadas com Inversão de Dependência
flowchart TD
ROUTER["Gin Router / Endpoints"]
--> HANDLER["Handler Layer (Extrai DTOs & Converte HTTP)"]
--> SERVICE_INT["Service Interface (Regras de Negócio Puras)"]
SERVICE_INT --> SERVICE_IMPL["Service Implementation"]
SERVICE_IMPL --> REPO_INT["Repository Interface (Abstração de Dados)"]
REPO_INT --> REPO_GORM["GORM Repository Implementation"]
REPO_GORM --> DB["PostgreSQL / MySQL"]
style ROUTER fill:#ede7f6,stroke:#512da8,stroke-width:2px
style HANDLER fill:#fff3e0,stroke:#ff9800,stroke-width:1.5px
style SERVICE_INT fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
style SERVICE_IMPL fill:#e8f5e9,stroke:#4caf50,stroke-width:1.5px
style REPO_INT fill:#e1f5fe,stroke:#03a9f4,stroke-width:2px
style REPO_GORM fill:#c8e6c9,stroke:#2e7d32,stroke-width:1.5px
style DB fill:#b2dfdb,stroke:#00796b,stroke-width:2px
🎯 Slide 1: Definição de Contratos via Interfaces em Go
type UsuarioRepository interface {
Criar(usuario *models.Usuario) error
BuscarPorID(id uint) (*models.Usuario, error)
BuscarPorEmail(email string) (*models.Usuario, error)
}
type UsuarioService interface {
Cadastrar(dto dtos.CriarUsuarioDTO) (*dtos.UsuarioResponseDTO, error)
}
🎯 Slide 2: Injeção de Dependências por Construtor (NewHandler)
type UsuarioHandler struct {
service services.UsuarioService
}
func NewUsuarioHandler(service services.UsuarioService) *UsuarioHandler {
return &UsuarioHandler{service: service}
}
- Desacoplamento total: os Handlers dependem apenas de interfaces, facilitando a criação de mocks em testes unitários!
🎯 Slide 3: Benefícios na Manutenção
- Mudar de GORM para SQL puro ou MongoDB afeta apenas a camada de repositório, deixando intactos os Handlers e regras de negócio do Service.
Slide 1 de 1
Atalhos: ← → Navegar • Espaço Avançar • F Tela Cheia • Home Início • End Fim