kratos-wire-provider

alecszaharia's avatarfrom alecszaharia

Manages Wire dependency injection providers in go-kratos microservices. Adds constructors to ProviderSets and regenerates Wire code. Use when integrating new components into dependency injection.

⭐0stars🔀0forks📁View on GitHub🕐Updated Dec 29, 2025

When & Why to Use This Skill

The kratos-wire-provider skill is a specialized utility designed to automate dependency injection management within Go-Kratos microservices. It streamlines the development workflow by managing Google Wire ProviderSets across various architectural layers—including Data, Biz, Service, and Server. By automatically adding constructors and triggering code regeneration, it ensures that microservice dependencies are correctly wired, type-safe, and consistent with the framework's best practices, significantly reducing manual boilerplate and configuration errors.

Use Cases

  • Integrating New Components: Automatically adding new repository, use case, or service constructors to the appropriate ProviderSet when expanding microservice features.
  • Automated Wire Regeneration: Executing the 'make generate' command and managing the lifecycle of wire_gen.go to ensure the dependency graph is always up-to-date.
  • Layered Architecture Management: Correctly placing providers within the specific Kratos layers (internal/data, internal/biz, internal/service) to maintain clean architectural boundaries.
  • Dependency Troubleshooting: Resolving common Wire-related issues such as 'no provider found' or circular dependencies by auditing and fixing ProviderSet definitions.
  • Codebase Standardization: Enforcing consistent ordering and naming conventions within ProviderSets to improve code readability and maintainability across large Go projects.
namekratos-wire-provider
descriptionManages Wire dependency injection providers in go-kratos microservices. Adds constructors to ProviderSets and regenerates Wire code. Use when integrating new components into dependency injection.
Add constructor functions to appropriate Wire ProviderSets and regenerate dependency injection code for go-kratos microservices. To add a provider to the biz layer:
  1. Add to ProviderSet in internal/biz/biz.go:
var ProviderSet = wire.NewSet(
	NewSymbolValidator,
	NewSymbolUseCase,
	NewProductUseCase,  // Add your constructor
)
  1. Regenerate Wire code:
cd services/{service-name}
make generate
## ProviderSet Locations

Data Layer (internal/data/data.go):

  • Repository implementations
  • Database connection
  • Transaction manager
var ProviderSet = wire.NewSet(
	NewData,           // Database setup
	NewTransaction,    // Transaction manager
	repo.NewSymbolRepo,
	repo.NewProductRepo,
)

Business Layer (internal/biz/biz.go):

  • Use cases
  • Validators
var ProviderSet = wire.NewSet(
	NewSymbolValidator,
	NewSymbolUseCase,
	NewProductUseCase,
)

Service Layer (internal/service/service.go):

  • Service handlers
var ProviderSet = wire.NewSet(
	NewSymbolService,
	NewProductService,
)

Server Layer (internal/server/server.go):

  • HTTP and gRPC servers
var ProviderSet = wire.NewSet(
	NewHTTPServer,
	NewGRPCServer,
)
## Wire Generation Process

File: cmd/{service}/wire.go defines dependencies:

//go:build wireinject

func InitApp(*conf.Server, *conf.Data, log.Logger) (*App, error) {
	wire.Build(
		server.ProviderSet,
		service.ProviderSet,
		biz.ProviderSet,
		data.ProviderSet,
		newApp,
	)
	return &App{}, nil
}

Generated: cmd/{service}/wire_gen.go (auto-generated, never edit)

Command: make generate runs:

GOWORK=off go generate ./cmd/{service}/...
## Provider Ordering

Within ProviderSet:

  • Alphabetical order (recommended)
  • OR grouped by functionality
  • Be consistent with existing pattern

Between ProviderSets (in wire.Build):

  1. server.ProviderSet (outermost layer)
  2. service.ProviderSet
  3. biz.ProviderSet
  4. data.ProviderSet (innermost layer)
  5. newApp (application constructor)
## Common Tasks

Add new repository:

  1. Add constructor to data.ProviderSet
  2. Run make generate

Add new use case:

  1. Add constructor to biz.ProviderSet
  2. Run make generate

Add new service handler:

  1. Add constructor to service.ProviderSet
  2. Run make generate

Troubleshooting:

  • If Wire errors, check constructor signatures match interface returns
  • Ensure all dependencies are provided in ProviderSets
  • Check for circular dependencies
  • Run GOWORK=off go mod tidy if module issues
## Wire Annotations

Optional: Use Wire build tags for conditional providers:

//go:build wireinject
// +build wireinject

Providers must:

  • Return types that match interface declarations
  • Have all parameters available in dependency graph
  • Not create circular dependencies
Wire integration is successful when: - [ ] Constructor added to appropriate ProviderSet - [ ] Alphabetical/consistent ordering maintained - [ ] `make generate` completes without errors - [ ] `wire_gen.go` updated successfully - [ ] Service compiles: `make build` - [ ] Tests pass: `make test` - [ ] No circular dependency errors ## Common Wire Errors

"no provider found for X": → Add constructor for X to appropriate ProviderSet

"cycle in dependency graph": → Circular dependency detected, refactor to break cycle

"provider returns interface but is not implemented": → Check constructor returns interface type, struct implements all methods

"multiple providers for type": → Same type provided by multiple constructors, remove duplicate