inject

package
v0.16.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package inject provides dependency injection container setup using samber/do/v2.

The package uses the do.Package pattern to group shared services into a reusable module. Services are lazily initialized - they are created only when first requested via do.Invoke, improving application startup time.

Base injector usage:

injector := inject.GetInjector()           // Get singleton base injector
service, _ := do.Invoke[MyService](injector)  // Resolve service (creates if needed)

Command-specific injectors:

cmdInjector := inject.GetInjector().Clone()  // Create isolated scope
do.Provide(cmdInjector, NewCommandService)   // Add command-specific services

The base injector is initialized once using sync.Once and provides shared services (gateways, repositories, domain services) to all commands. Each command creates an isolated scope by cloning the base injector to avoid cross-command pollution.

Index

Constants

This section is empty.

Variables

AuthPackage groups all services specific to the auth command. Services are lazily initialized when first requested.

BasePackage groups all shared services for the application.

Services are registered with do.Lazy() wrappers, enabling lazy initialization. Each service is created only when first requested via do.Invoke, not during package initialization. This improves startup performance by deferring expensive initializations until actually needed.

The package includes:

  • adapter/gateway: File providers (Drive API, local filesystem)
  • domain/repository: Data access (nippo queries, commands, assets)
  • domain/service: Business logic (nippo facade, template service)

Note: Configuration is managed via the global core.Cfg variable initialized by core.InitConfig() at application startup, not through dependency injection.

Command-specific services (controllers, presenters, use cases) are registered separately in each command's injector file (e.g., build.go, clean.go).

BuildPackage groups all services specific to the build command. Services are lazily initialized when first requested.

CleanPackage groups all services specific to the clean command. Services are lazily initialized when first requested.

DeployPackage groups all services specific to the deploy command. Services are lazily initialized when first requested.

DoctorPackage groups all services specific to the doctor command.

FormatPackage groups all services specific to the format command. Services are lazily initialized when first requested.

InitPackage groups all services specific to the init command. Services are lazily initialized when first requested.

View Source
var InjectorAuth = do.New(BasePackage, AuthPackage)

InjectorAuth provides a DI container with both base and auth-specific services.

View Source
var InjectorBuild = do.New(BasePackage, BuildPackage)

InjectorBuild provides a DI container with both base and build-specific services.

View Source
var InjectorClean = do.New(BasePackage, CleanPackage)

InjectorClean provides a DI container with both base and clean-specific services.

View Source
var InjectorDeploy = do.New(BasePackage, DeployPackage)

InjectorDeploy provides a DI container with both base and deploy-specific services.

View Source
var InjectorDoctor = do.New(BasePackage, DoctorPackage)

InjectorDoctor provides a DI container with both base and doctor-specific services.

View Source
var InjectorFormat = do.New(BasePackage, FormatPackage)

InjectorFormat provides a DI container with both base and format-specific services.

View Source
var InjectorInit = do.New(BasePackage, InitPackage)

InjectorInit provides a DI container with both base and init-specific services.

View Source
var InjectorRoot = do.New(BasePackage, RootPackage)

InjectorRoot provides a DI container with both base and root-specific services.

View Source
var InjectorUpdate = do.New(BasePackage, UpdatePackage)

InjectorUpdate provides a DI container with both base and update-specific services.

RootPackage groups all services specific to the root command. Services are lazily initialized when first requested.

UpdatePackage groups all services specific to the update command. Services are lazily initialized when first requested.

Functions

func GetInjector added in v0.15.0

func GetInjector() *do.RootScope

GetInjector returns the singleton DI container with lazy initialization. It is thread-safe and initializes the container only once.

func NewTestInjector added in v0.15.0

func NewTestInjector(opts *TestBasePackageOptions) *do.RootScope

NewTestInjector creates a test injector with optional service replacements.

Usage:

// Use all default services
injector := inject.NewTestInjector(nil)

// Replace specific services with mocks
injector := inject.NewTestInjector(&inject.TestBasePackageOptions{
    DriveFileProvider: mockDriveProvider,
    Config: testConfig,
})

// Invoke services as normal
controller, _ := do.Invoke[controller.BuildController](injector)

Types

type TestBasePackageOptions added in v0.15.0

type TestBasePackageOptions struct {
	// core
	Config *core.Config

	// adapter/gateway
	DriveFileProvider gateway.DriveFileProvider
	LocalFileProvider gateway.LocalFileProvider

	// adapter/presenter
	ConsolePresenter       presenter.ConsolePresenter
	RootCommandPresenter   presenter.RootCommandPresenter
	CleanCommandPresenter  presenter.CleanCommandPresenter
	DeployCommandPresenter presenter.DeployCommandPresenter
	UpdateCommandPresenter presenter.UpdateCommandPresenter
	AuthPresenter          presenter.AuthPresenter
	DoctorPresenter        presenter.DoctorPresenter
	BuildCommandPresenter  presenter.BuildCommandPresenter
	FormatCommandPresenter presenter.FormatCommandPresenter
	InitSettingPresenter   presenter.InitSettingPresenter

	// domain/repository
	RemoteNippoQuery  repository.RemoteNippoQuery
	LocalNippoQuery   repository.LocalNippoQuery
	LocalNippoCommand repository.LocalNippoCommand
	AssetRepository   repository.AssetRepository

	// domain/service
	NippoFacade     service.NippoFacade
	TemplateService service.TemplateService
}

TestBasePackageOptions allows selective replacement of services in the base package. Any non-nil field will override the default implementation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL