golang-clean-architecture
Scannednpx machina-cli add skill saifoelloh/golang-best-practices-skill/clean-architecture --openclawGolang Clean Architecture
Audit Go services for Clean Architecture compliance. Ensures proper layering, dependency rules, and separation of concerns in gRPC → Usecase → Repository → Domain architectures.
When to Apply
Use this skill when:
- Auditing service architecture
- Reviewing new features for layer violations
- Refactoring toward Clean Architecture
- Code review for dependency rules
- Planning service structure
- Migrating to layered architecture
- Ensuring testability through dependency injection
Architecture Layers
┌─────────────────────────────────────┐
│ Delivery (gRPC/HTTP/GraphQL) │ ← Thin, no business logic
├─────────────────────────────────────┤
│ Usecase (Business Logic) │ ← Orchestration
├─────────────────────────────────────┤
│ Repository (Data Access) │ ← CRUD only
├─────────────────────────────────────┤
│ Domain (Entities/Interfaces) │ ← Pure business logic
└─────────────────────────────────────┘
Dependency Rule: Dependencies point INWARD only (toward domain).
Rules Covered (9 total)
High-Impact Patterns (4)
high-business-logic-handler- Keep delivery layer thinhigh-business-logic-repository- No business logic in data layerhigh-constructor-creates-deps- Inject dependencies, don't createhigh-transaction-in-repository- Transactions belong in usecase
Architecture Rules (5)
arch-domain-import-infra- Domain must not import infrastructurearch-concrete-dependency- Depend on interfaces, not concrete typesarch-repository-business-logic- Repositories do CRUD onlyarch-usecase-orchestration- Usecases orchestrate, entities decidearch-interface-segregation- Small, consumer-defined interfaces
Common Violations
❌ Business Logic in Handler
// gRPC handler doing calculations
func (h *Handler) CreateOrder(ctx context.Context, req *pb.Request) (*pb.Response, error) {
total := req.Price * req.Quantity // BAD: calculation in handler
discount := total * 0.1 // BAD: business rules in delivery layer
order := &domain.Order{
Total: total - discount,
}
return h.orderRepo.Save(ctx, order)
}
✅ Business Logic in Usecase
// Handler delegates to usecase
func (h *Handler) CreateOrder(ctx context.Context, req *pb.Request) (*pb.Response, error) {
order, err := h.orderUsecase.Create(ctx, req.Price, req.Quantity)
if err != nil {
return nil, err
}
return &pb.Response{OrderId: order.ID}, nil
}
// Usecase contains business logic
func (u *OrderUsecase) Create(ctx context.Context, price, quantity int) (*domain.Order, error) {
total := price * quantity // GOOD: calculation in usecase
discount := total * 0.1 // GOOD: business rules in usecase
order := &domain.Order{
Total: total - discount,
}
return u.orderRepo.Save(ctx, order)
}
❌ Repository with Business Logic
// Repository doing validation and business rules
func (r *OrderRepo) Save(ctx context.Context, order *domain.Order) error {
if order.Total < 0 { // BAD: validation in repository
return errors.New("invalid total")
}
if order.Total > 1000000 { // BAD: business rule in repository
order.Status = "needs_approval" // BAD: state change in repository
}
return r.db.Create(order)
}
✅ Repository Does CRUD Only
// Repository only handles data persistence
func (r *OrderRepo) Save(ctx context.Context, order *domain.Order) error {
return r.db.Create(order) // GOOD: simple CRUD
}
// Validation happens in usecase or domain entity
func (u *OrderUsecase) Create(ctx context.Context, price, quantity int) (*domain.Order, error) {
order := domain.NewOrder(price, quantity) // Entity validates itself
if err := order.Validate(); err != nil { // GOOD: validation in domain
return nil, err
}
if order.NeedsApproval() { // GOOD: business rule in domain
order.Status = "needs_approval"
}
return u.orderRepo.Save(ctx, order)
}
Trigger Phrases
This skill activates when you say:
- "Audit architecture"
- "Check layer dependencies"
- "Review Clean Architecture"
- "Verify separation of concerns"
- "Check dependency rules"
- "Review usecase/repository pattern"
- "Check for layer violations"
- "Audit service structure"
How to Use
For Architecture Audit
- Identify all layers in the codebase
- Check dependency directions (must point inward)
- Verify each layer's responsibilities
- Flag violations with specific rule references
For Code Review
- Identify which layer the code belongs to
- Check against layer-specific rules
- Verify dependencies are injected, not created
- Ensure interfaces are defined by consumers
Output Format
## Architecture Violations: X
### [Rule Name] (File: path/to/file.go)
**Layer**: Delivery / Usecase / Repository / Domain
**Issue**: Brief description of violation
**Impact**: Tight coupling / Untestable / Wrong responsibility
**Fix**: Suggested correction
**Example**:
```go
// Corrected code
Related Skills
- golang-design-patterns - For refactoring large usecases
- golang-idiomatic-go - For interface design patterns
- golang-error-handling - For context propagation across layers
Philosophy
Based on Uncle Bob's Clean Architecture:
- Independence - Business rules don't depend on frameworks, UI, or databases
- Testability - Business logic can be tested without external dependencies
- Flexibility - Easy to swap implementations (e.g., change database)
- Maintainability - Clear boundaries make changes localized
Key Principle: The inner circles know nothing about the outer circles.
Notes
- Rules enforce separation of concerns in Go services
- Particularly focused on gRPC/usecase/repository pattern
- Emphasizes dependency injection for testability
- All examples follow Clean Architecture principles
Source
git clone https://github.com/saifoelloh/golang-best-practices-skill/blob/main/clean-architecture/SKILL.mdView on GitHub Overview
This skill audits Go services for Clean Architecture compliance, ensuring proper layering and dependency rules across Delivery, Usecase, Repository, and Domain. It targets gRPC/usecase/repository patterns to enforce separation of concerns and dependency inversion.
How This Skill Works
It analyzes the architectural layers and enforces the inward dependency rule toward the Domain. It flags violations like business logic in the delivery or repository layers and checks for proper interface usage and where transactions belong (usecase).
When to Use It
- Auditing service architecture
- Reviewing new features for layer violations
- Refactoring toward Clean Architecture
- Planning service structure
- Ensuring testability through dependency injection
Quick Start
- Step 1: Audit current layers against the Clean Architecture rules (Delivery, Usecase, Repository, Domain)
- Step 2: Identify violations (e.g., business logic in handlers or repositories; improper dependencies)
- Step 3: Refactor: move logic to usecase/domain, inject dependencies, and re-run the audit
Best Practices
- Keep the delivery layer thin to avoid business logic in handlers
- Inject dependencies via constructors; don’t create them in delivery or usecase
- Place transactions in the usecase, not the repository
- Repositories should CRUD only; do not embed business rules
- Domain should be independent from infrastructure and depend on interfaces
Example Use Cases
- ❌ Business logic in Handler: calculations performed in the delivery layer
- ✅ Move business logic into the Usecase: usecases orchestrate business rules
- ❌ Repository with business logic: validation or state changes inside repository
- ✅ Repository Does CRUD Only: data access remains pure CRUD operations
- ❌ Domain importing infrastructure: violates inward dependency rule