Procyon|Docs
ComponentsRegistration

Constructors

Write constructors that the component container can understand.

A component constructor is a function that returns the component instance.

repository.go
type UserRepository struct{}

func NewUserRepository() *UserRepository {
	return &UserRepository{}
}

The return value must be a struct, pointer to struct, or interface. Constructor parameters are treated as dependencies.

service.go
type UserService struct {
	repo *UserRepository
}

func NewUserService(repo *UserRepository) *UserService {
	return &UserService{repo: repo}
}

Keep startup work out of constructors

Constructors should assemble values. They should not open long-lived connections, start workers, or perform heavy validation.

cache.go
func NewCache(client CacheClient) *Cache {
	return &Cache{client: client}
}

Use lifecycle hooks when a component needs runtime setup after dependencies are available.

cache.go
func (c *Cache) Init(ctx context.Context) error {
	return c.client.Ping(ctx)
}

Constructor guidance

  • Return one component value.
  • Put required dependencies in parameters.
  • Keep constructors deterministic.
  • Use interfaces when the dependency is a capability.
  • Use lifecycle hooks for runtime setup.