Components
Register Components
Create component definitions from constructors.
Register Components
Register components from package initialization code so Procyon can discover them during startup.
package user
import "codnect.io/procyon/component"
func init() {
component.Register(NewUserRepository)
component.Register(NewUserService)
}
type UserRepository struct{}
func NewUserRepository() *UserRepository {
return &UserRepository{}
}
type UserService struct {
repo *UserRepository
}
func NewUserService(repo *UserRepository) *UserService {
return &UserService{repo: repo}
}By default, Procyon generates the component name from the constructor return
type. For example, NewUserService() *UserService becomes userService.
Name components explicitly
Use component.WithName when the generated name is not the name you want to use
or when multiple components share a type.
func init() {
component.Register(
NewPrimaryMailer,
component.WithName("primaryMailer"),
)
}
func NewPrimaryMailer() Mailer {
return &SmtpMailer{}
}Choose a scope
Components are singletons by default. Use prototype scope when every resolution should create a fresh instance.
func init() {
component.Register(NewRequestState, component.AsPrototype())
}
type RequestState struct {
Values map[string]string
}
func NewRequestState() *RequestState {
return &RequestState{Values: make(map[string]string)}
}You can also set the scope directly with component.WithScope.
