Claude-skill-registry kratos-biz-layer
Generates business logic layer components (use cases, repository interfaces, business models, validators) for go-kratos microservices following Clean Architecture. Use when implementing domain logic, creating entity CRUD operations, adding validation rules, or orchestrating data access in kratos services.
git clone https://github.com/majiayu000/claude-skill-registry
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/kratos-biz-layer" ~/.claude/skills/majiayu000-claude-skill-registry-kratos-biz-layer && rm -rf "$T"
skills/data/kratos-biz-layer/SKILL.md<essential_principles>
How Kratos Business Layer Works
Clean Architecture Dependency Rule
The business layer (biz) is the core of Clean Architecture in Kratos microservices:
Dependency Flow: Service → Biz → Data
- Biz defines interfaces that data layer implements
- Biz contains business logic independent of frameworks
- Biz uses domain models not ORM entities
Domain-Driven Structure (Current Pattern)
The biz layer follows domain-driven design with these subdirectories:
1. Domain Layer (
internal/biz/domain/)
- Domain entities with validation tagsmodels.go
- Repository, use case, and event publisher interfacesinterfaces.go
- Domain errors (ErrSymbolNotFound) and data layer errors (ErrDataNotFound)errors.go
2. Use Case Layer (
internal/biz/{entity}/)
- Use case implementationusecase.go
- Use case tests with event assertionsusecase_test.go
- Validation logic factoryvalidator.go
3. Event Layer (
internal/biz/event/) - Optional
- Event payload transformationsmapper.go
- Event topic constantstopics.go
4. Provider (
internal/biz/provider.go)
- Wire ProviderSet for dependency injection
Error Separation Pattern
Data Layer Errors (returned by repositories):
- Record not founddomain.ErrDataNotFound
- Unique constraint violationdomain.ErrDataDuplicateEntry
- Generic database errordomain.ErrDataDatabase
Domain Errors (returned by use cases):
- Symbol not found (business error)domain.ErrSymbolNotFound
- Duplicate symbol (business error)domain.ErrDuplicateSymbol
- Database operation faileddomain.ErrDatabaseOperation
Pattern: Repositories return data errors, use cases map to domain errors via
toDomainError() helper.
Event Publishing Pattern
Use cases integrate event publishing for domain events:
- Accept
interfacedomain.{Entity}EventPublisher - Publish events within transactions
- Transaction rolls back if publishing fails
Example:
err := uc.tm.InTx(ctx, func(ctx context.Context) error { symbol, err = uc.repo.Create(ctx, s) if err != nil { return err } return uc.pub.PublishSymbolCreated(ctx, symbol) })
Wire Dependency Injection
All constructors must be added to
ProviderSet in internal/biz/provider.go:
var ProviderSet = wire.NewSet(symbol.NewValidator, symbol.NewUseCase)
After changes, run
make generate to regenerate Wire code.
</essential_principles>
<intake>
What would you like to do?
- Create a new entity (use case + interface + model + validator)
- Add methods to existing use case
- Create standalone validator
- View examples and patterns
Wait for response before proceeding. </intake>
<routing> | Response | Workflow | |----------|----------| | 1, "create", "new entity", "new" | `workflows/create-entity.md` | | 2, "add method", "add", "extend" | `workflows/add-methods.md` | | 3, "validator", "validation" | `workflows/create-validator.md` | | 4, "examples", "patterns", "help" | `workflows/view-examples.md` |After reading the workflow, follow it exactly. </routing>
<reference_index> All domain knowledge in
references/:
Core Patterns: use-case-pattern.md, interface-pattern.md, model-pattern.md Code Style: naming-conventions.md
Additional patterns (error handling, logging, transactions, testing) are covered within use-case-pattern.md. </reference_index>
<workflows_index>
| Workflow | Purpose |
|---|---|
| create-entity.md | Generate complete biz layer for new entity |
| add-methods.md | Add CRUD methods to existing use case |
| create-validator.md | Create standalone validator |
| view-examples.md | Show patterns and examples |
| </workflows_index> |
<success_criteria> Business layer code is correct when:
Structure:
- Domain models in
internal/biz/domain/models.go - Interfaces in
internal/biz/domain/interfaces.go - Errors in
internal/biz/domain/errors.go - Use case in
internal/biz/{entity}/usecase.go - Validator in
internal/biz/{entity}/validator.go - Tests in
internal/biz/{entity}/usecase_test.go
Use Case Implementation:
- Struct has repo, pub, log, validator, tm fields
- Constructor returns interface type (domain.{Entity}UseCase)
- Methods accept context.Context as first parameter
- Validation happens before repository calls
- Data errors mapped to domain errors via
toDomainError() - Events published within transactions
- All errors logged with context
Domain Layer:
- Repository interface in
domain/interfaces.go - Event publisher interface in
(if events)domain/interfaces.go - Business model in
with validation tagsdomain/models.go - Domain errors (ErrSymbolNotFound) separated from data errors (ErrDataNotFound)
Wire Integration:
- Constructor added to ProviderSet in
internal/biz/provider.go - User reminded to run
</success_criteria>make generate