Components
Wire Dependencies
Inject dependencies through constructors and qualifiers.
Wire Dependencies
Constructor parameters are resolved from the container. Keep dependencies in the constructor signature and return the component instance.
type Mailer interface {
Send(to string, body string) error
}
type WelcomeService struct {
mailer Mailer
}
func NewWelcomeService(mailer Mailer) *WelcomeService {
return &WelcomeService{mailer: mailer}
}If there is exactly one component assignable to Mailer, Procyon injects it
automatically.
Qualify constructor arguments
When a constructor has an argument that should use a specific named component, attach a qualifier to that parameter.
func init() {
component.Register(NewPrimaryMailer, component.WithName("primaryMailer"))
component.Register(NewAuditMailer, component.WithName("auditMailer"))
component.Register(
NewNotificationService,
component.WithQualifierFor[Mailer]("primaryMailer"),
)
}
type NotificationService struct {
mailer Mailer
}
func NewNotificationService(mailer Mailer) *NotificationService {
return &NotificationService{mailer: mailer}
}For constructors with repeated parameter types, use the argument index instead.
component.Register(
NewRelayService,
component.WithQualifierAt(0, "primaryMailer"),
component.WithQualifierAt(1, "auditMailer"),
)