ComponentsResolving Components
Resolve Components
Resolve components manually from a container when constructor injection is not available.
Most application code should use constructor injection. Manual resolution is for boundary code: runners, adapters, framework integrations, and places where the runtime container is the API you receive.
type ReportRunner struct {
ctx runtime.Context
}
func NewReportRunner(ctx runtime.Context) *ReportRunner {
return &ReportRunner{ctx: ctx}
}Manual resolution usually follows this shape:
- Receive or access a runtime container.
- Resolve the component by type, name, or collection.
- Handle resolution errors explicitly.
- Use the resolved component at the boundary.
| Helper | Use it for |
|---|---|
component.ResolveType[T](ctx, container) | resolving one component by type |
component.Resolve[T](ctx, container, name) | resolving one named component as T |
component.ResolveAll[T](ctx, container) | resolving every component assignable to T |
component.CanResolve(container, name) | checking whether a name can resolve |
component.CanResolveType[T](container) | checking whether a type can resolve |
Prefer constructor injection for regular services. Reach for these helpers when code lives at a runtime boundary.
