Dependency Injection
A controller needs a service before it can handle a request. In GoForj, you declare that dependency in the controller's constructor and register the service constructor in the App's provider set. Wire generates the Go calls that connect them.
One Dependency, End to End
The first route guide adds a users service and controller to the default app. This excerpt from internal/users/controller.go declares the connection.
func NewController(service *Service) *Controller {
return &Controller{service: service}
}NewController requires a *users.Service. The guide registers users.NewService in the existing appSet in app/wire/inject_services_app.go. The controller generator has already registered users.NewController in app/wire/inject_http_controllers_app.go.
When you run forj build, Wire matches the service constructor's return type to the controller's parameter. It writes calls to users.NewService() and users.NewController(service) into app/wire/wire_gen.go. Those calls run when the App is constructed. Each request then uses the service held by the controller.
A provider is the constructor or function that supplies a value. A provider set groups those functions for Wire. The graph is the set of dependencies Wire follows to construct the App. Wire determines construction order from constructor parameters.
Where the Graph Lives
Follow the users connection through these files.
| File | Role |
|---|---|
internal/users/service.go | Defines NewService and the application behavior |
internal/users/controller.go | Defines NewController, its required service, and HTTP handlers |
app/wire/inject_services_app.go | Registers the service constructor in appSet |
app/wire/inject_http_controllers_app.go | Registers the controller constructor |
app/wire/wire_gen.go | Contains Wire's generated constructor calls |
app/routes.go | Composes the app's routes |
Edit constructors and the matching App-owned _app.go sets. Regenerate wire_gen.go with forj build. Repository constructors belong in inject_repositories_app.go. Wiring Recipes maps the other provider files.
Reading a Failure
If Wire reports a missing *users.Service, add users.NewService to appSet. Then run this from the Project root.
forj route:listGoForj checks the current wiring before listing routes. Look for /api/v1/users/:id with method GET. See Reading Wire Errors for other provider errors.
Construction and Startup
A provider can return (T, error). An error stops App construction, so configuration errors should name the missing setting or resource. Only providers required by the graph run during construction.
Keep request behavior in the service. Put process startup and shutdown in lifecycle hooks or the owning runtime. A successful build checks the wiring. Starting the App checks the configuration and runtime behavior.
More Provider Choices
Keep dependencies explicit in constructors so tests can supply them directly. Each App has its own graph and constructs its own service instances. See Wiring Recipes when a provider needs configuration, an interface binding, or a grouped set.
Next Steps
Continue to App Lifecycle to follow the constructed App through startup and shutdown. Use the standalone Wire reference when you need its provider-set API.