Providers
Providers are injectable services in Ginject. Any struct implementing NewProvider() can be injected into controllers and other providers automatically.
Providers
A Provider is any Go struct that implements the NewProvider() core.Provider interface. Providers encapsulate business logic, database access, external API calls, or any shared functionality.
Creating a Provider
The NewProvider() method must return core.Provider and returns s (the receiver value). This is required for the DI container to recognize the struct.
Injection Mechanics
Ginject uses reflection to wire providers. If a controller or another provider has a field whose type matches a registered provider, it is injected automatically.
No field tags are needed. The match is done on the concrete type, not the field name.
Provider-to-Provider Injection
Providers can depend on other providers:
Interface-Based Providers
You can register a provider against an interface. This is useful for testing or when you have multiple implementations:
Scoping
Providers are singleton-scoped within the module that declares them. If two modules each declare their own UserService, they get separate instances. Use a global module to share a single instance.
Accessing the Built-in Logger
The framework's structured logger is available as an injectable interface. Declare it on your provider: