Vessel

Type-safe dependency injection container for Go

Vessel is a standalone, type-safe dependency injection container for Go. It provides constructor-based injection, service lifecycle management, flexible resolution strategies, and support for lazy and optional dependencies. Vessel is the DI engine that powers Forge, but it can be used independently in any Go project.

Installation

go get github.com/xraph/vessel

Use constructor injection by type as the default pattern. Dependencies are resolved automatically from function parameter types.

import "github.com/xraph/vessel"

c := vessel.New()

vessel.ProvideConstructor(c, func() *Database {
    return &Database{DSN: "postgres://localhost:5432/app"}
})

vessel.ProvideConstructor(c, func(db *Database) *UserService {
    return &UserService{db: db}
})

// Resolve by type -- no string keys needed
userService, err := vessel.InjectType[*UserService](c)

This keeps your dependency graph explicit, avoids string-key drift, and provides compile-time type safety.

Key Features

  • Type-safe generics -- compile-time type checking via Go generics
  • Constructor injection -- Uber dig-style automatic dependency resolution with In/Out structs
  • Multiple lifecycles -- singleton, transient, and scoped services
  • Typed service keys -- ServiceKey[T] for compile-time safe registration and resolution
  • Lazy and optional dependencies -- Lazy[T], OptionalLazy[T], Provider[T]
  • Service lifecycle -- built-in Start/Stop/Health management with topological ordering
  • Circular dependency detection -- detected at resolution time with clear error paths
  • Concurrency safe -- thread-safe container operations
  • Middleware hooks -- intercept resolve and lifecycle events
  • Service discovery -- query and filter services by lifecycle, group, or metadata
  • Batch registration -- register multiple services in a single call

Documentation

How is this guide?

On this page