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 ¶
var AuthPackage = do.Package( do.Lazy(controller.NewAuthController), do.Lazy(port.NewAuthUseCaseBus), do.Lazy(interactor.NewAuthInteractor), do.Lazy(presenter.NewAuthPresenter), )
AuthPackage groups all services specific to the auth command. Services are lazily initialized when first requested.
var BasePackage = do.Package( do.Lazy(gateway.NewDriveFileProvider), do.Lazy(gateway.NewLocalFileProvider), do.Lazy(presenter.NewConsolePresenter), do.Lazy(repository.NewRemoteNippoQuery), do.Lazy(repository.NewLocalNippoQuery), do.Lazy(repository.NewLocalNippoCommand), do.Lazy(repository.NewAssetRepository), do.Lazy(service.NewNippoFacade), do.Lazy(service.NewTemplateService), )
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).
var BuildPackage = do.Package( do.Lazy(controller.NewBuildController), do.Lazy(port.NewBuildUseCaseBus), do.Lazy(interactor.NewBuildCommandInteractor), do.Lazy(presenter.NewBuildCommandPresenter), )
BuildPackage groups all services specific to the build command. Services are lazily initialized when first requested.
var CleanPackage = do.Package( do.Lazy(controller.NewCleanController), do.Lazy(port.NewCleanUseCaseBus), do.Lazy(interactor.NewCleanCommandInteractor), do.Lazy(presenter.NewCleanCommandPresenter), )
CleanPackage groups all services specific to the clean command. Services are lazily initialized when first requested.
var DeployPackage = do.Package( do.Lazy(controller.NewDeployController), do.Lazy(port.NewDeployUseCaseBus), do.Lazy(interactor.NewDeployCommandInteractor), do.Lazy(presenter.NewDeployCommandPresenter), )
DeployPackage groups all services specific to the deploy command. Services are lazily initialized when first requested.
var DoctorPackage = do.Package( do.Lazy(controller.NewDoctorController), do.Lazy(func(i do.Injector) (port.DoctorUseCase, error) { return interactor.NewDoctorInteractor(i) }), do.Lazy(presenter.NewDoctorPresenter), )
DoctorPackage groups all services specific to the doctor command.
var FormatPackage = do.Package( do.Lazy(controller.NewFormatController), do.Lazy(port.NewFormatUseCaseBus), do.Lazy(interactor.NewFormatCommandInteractor), do.Lazy(presenter.NewFormatCommandPresenter), )
FormatPackage groups all services specific to the format command. Services are lazily initialized when first requested.
var InitPackage = do.Package( do.Lazy(controller.NewInitController), do.Lazy(port.NewInitUseCaseBus), do.Lazy(interactor.NewInitSettingInteractor), do.Lazy(presenter.NewInitSettingPresenter), do.Lazy(view.NewInitViewProvider), do.Lazy(view.NewConfigureProjectView), )
InitPackage groups all services specific to the init command. Services are lazily initialized when first requested.
var InjectorAuth = do.New(BasePackage, AuthPackage)
InjectorAuth provides a DI container with both base and auth-specific services.
var InjectorBuild = do.New(BasePackage, BuildPackage)
InjectorBuild provides a DI container with both base and build-specific services.
var InjectorClean = do.New(BasePackage, CleanPackage)
InjectorClean provides a DI container with both base and clean-specific services.
var InjectorDeploy = do.New(BasePackage, DeployPackage)
InjectorDeploy provides a DI container with both base and deploy-specific services.
var InjectorDoctor = do.New(BasePackage, DoctorPackage)
InjectorDoctor provides a DI container with both base and doctor-specific services.
var InjectorFormat = do.New(BasePackage, FormatPackage)
InjectorFormat provides a DI container with both base and format-specific services.
var InjectorInit = do.New(BasePackage, InitPackage)
InjectorInit provides a DI container with both base and init-specific services.
var InjectorRoot = do.New(BasePackage, RootPackage)
InjectorRoot provides a DI container with both base and root-specific services.
var InjectorUpdate = do.New(BasePackage, UpdatePackage)
InjectorUpdate provides a DI container with both base and update-specific services.
var RootPackage = do.Package( do.Lazy(controller.NewRootController), do.Lazy(port.NewRootUseCaseBus), do.Lazy(interactor.NewRootCommandInteractor), do.Lazy(presenter.NewRootCommandPresenter), )
RootPackage groups all services specific to the root command. Services are lazily initialized when first requested.
var UpdatePackage = do.Package( do.Lazy(controller.NewUpdateController), do.Lazy(port.NewUpdateUseCaseBus), do.Lazy(interactor.NewUpdateCommandInteractor), do.Lazy(presenter.NewUpdateCommandPresenter), )
UpdatePackage groups all services specific to the update command. Services are lazily initialized when first requested.
Functions ¶
func GetInjector ¶ added in v0.15.0
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.