Wiring Recipes
Use this page when you created a Go package and need to connect its constructors to an app.
GoForj apps use explicit provider sets. Your package owns constructors. The app's wire package imports those constructors and adds them to the right set.
Quick Map
| You built | Register it in | Typical provider |
|---|---|---|
| Application service | app/wire/inject_services_app.go | billing.NewService |
| Outbound adapter or gateway | app/wire/inject_services_app.go | billing.ProvideGateway |
| Repository | app/wire/inject_repositories_app.go when present | reports.NewRepository |
| HTTP controller | app/wire/inject_http_controllers_app.go | users.NewController |
| App command | app/wire/inject_cmd_app.go and app/commands.go | reports.NewReconcileCommand |
| Job handler | app/wire/inject_jobs_app.go when jobs are enabled | reports.NewGenerateHandler |
| Schedule | app/wire/inject_schedules_app.go | reports.NewDailySchedule and its AppSchedules entry |
| Event subscriber | app/wire/inject_subscribers_app.go | billing.NewInvoicePaidSubscriber |
| Named resource adapter | Usually app/wire/inject_services_app.go | provideUploadsDisk |
For an additional app, replace app/... with the owning app's app/<name>/....
Use the most specific generated set that owns the surface. If a generated file is not present, the app probably does not have that component enabled.
Generated Resources
When a resource has a make command, use it before editing provider sets by hand. The command creates the resource and updates the active App's generated wiring boundaries.
The Make Command Reference shows each generator in the same Usage / Files touched / Generated code format. This page begins where those flows stop: wiring the application services and adapters that generated entry points depend on.
Service and Adapter
Application packages usually own the service and any adapter it depends on:
internal/billing/gateway.go
internal/billing/provider.go
internal/billing/service.go
app/wire/inject_services_app.goThen wire those constructors from:
app/wire/inject_services_app.gopackage wire
import (
"github.com/goforj/wire"
"myapp/internal/billing"
)
var appSet = wire.NewSet(
// existing app providers...
billing.ProvideGateway,
billing.NewService,
)Wire can construct *billing.Service because billing.ProvideGateway provides the *billing.Gateway that billing.NewService receives.
HTTP Controller
The controller verification workflow shows the command, files, and injected route registration together.
The controller can depend on an application service already provided by the app service set. If Wire cannot provide that service, add the service constructor to app/wire/inject_services_app.go.
Verify the result:
forj build
forj route:listCommand
The command creation workflow shows the command, generated type, provider, and App command collection together.
Command constructors should receive application services as parameters. They should not create repositories, managers, clients, or services themselves.
Commands also need to be exposed through the generated command collection. See Commands for the command-specific registration path.
Named Resource
Named resources often need a small provider function that selects one generated resource from a manager:
package wire
import (
"github.com/goforj/storage"
"myapp/internal/storages"
"myapp/internal/uploads"
)
var appSet = wire.NewSet(
// existing app providers...
provideUploadsDisk,
uploads.NewService,
)
func provideUploadsDisk(manager *storages.Manager) storage.Storage {
return manager.Uploads()
}The service receives the specific resource it needs instead of reaching into the manager itself.
After Editing
Regenerate the graph after changing providers or generated component files:
forj buildforj build refreshes generated code, runs Wire, indexes APIs, and builds the app binary.
Common Mistakes
Common mistakes
- Do not add constructors to
app/wire/wire_gen.go; it is generated output. - Do not register a controller in the service set when it belongs in the HTTP controller set.
- Do not create dependencies inside commands or controllers when they should be constructor parameters.
- Do not use package globals to avoid wiring a provider.
- Do not register two providers for the same raw type when domain-specific adapter types would make the graph clearer.
Next Steps
- Make Command Reference explains the generated resource flow.
- Provider Patterns shows how to shape providers in application packages.
- Reading Wire Errors explains how to debug missing and duplicate providers.
- Dependency Injection explains the generated graph model.