depinject

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 1 Imported by: 0

README

Depinject

Go Reference

This project is a runtime-based dependency injection framework written in Go designed to simplify the management of dependencies in Go applications. It provides a robust and flexible way to construct your application components and resolve their dependencies at runtime.

Feature Support

Currently the framework supports the following features:

  • Constructors which return types exactly as requested by another's constructor.
  • Constructors which return types which implement interfaces requested by another's constructor.
  • Constructors which return types which match a variadic argument requested by another's constructor.
  • Constructors which return types which match a generic type requested by another's constructor.
  • Supplying values directly into the container.
  • Sentinel structs for constructor inputs and outputs.

Getting Started

Clone the Project

To get started with this project, clone the repository to your local machine:

$ git clone https://github.com/skjdfhkskjds/depinject
$ cd depinject
Build and Run

To run the project and see the dependency injection in action:

$ go test ./examples/basic_test.go

Example Usage

Here is a simple example demonstrating how to use the dependency injection framework:

package main

import (
    "fmt"
    "github.com/skjdfhkskjds/depinject"
)

func NewBar(_ *Foo) *Bar {
    return &Bar{}
}

func NewFooBar(_ *Foo, _ *Bar) *FooBar {
    return &FooBar{}
}

func main() {
	container := depinject.NewContainer()

	// Supply a value into the container directly.
	if err := container.Supply(&Foo{}); err != nil {
		panic(err)
	}

	// Provide a set of constructors into the container.
	if err := container.Provide(
		NewBar,
		NewFooBar,
	); err != nil {
		panic(err)
	}

	// Invoke a function with the dependencies injected
	// to retrieve the FooBar instance.
	var fooBar *FooBar
	if err := container.Invoke(&fooBar); err != nil {
		panic(err)
	}
	fooBar.Print()
}

For more examples, check out the examples directory.

Documentation

For more detailed documentation, refer to the code comments and the structured documentation within each package.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NewContainer returns a new, valid container.
	NewContainer = depinject.NewContainer

	// DefaultContainer returns a new container with the default options.
	DefaultContainer = depinject.DefaultContainer
)

Available functions from this package.

Functions

func Invoke

func Invoke(outputs ...any) error

Invoke invokes the given functions with the dependencies injected from the global container instance.

func Provide

func Provide(constructors ...any) error

Provide provides the given constructors into the global container instance.

func Supply

func Supply(values ...any) error

Supply supplies the given values into the global container instance.

Types

type Container

type Container = depinject.Container

Container is the main entrypoint of this depinject library. Its usage should be as follows:

container := NewContainer()
container.Provide(constructor1, constructor2, ...)
container.Invoke(func(dep1, dep2, ...) {
	// do something with the dependencies
})

type In

type In = depinject.In

In is a sentinel type used to indicate that a struct is actually a container for various types that should be included in the constructor's argument list.

type Out

type Out = depinject.Out

Out is a sentinel type used to indicate that a struct is actually a container for various types that should be included in the constructor's output list.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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