Giter Club home page Giter Club logo

di's Introduction

di

di implements a container for dependency injection for Go without using reflection.

How does it not use reflection?

The problem is mapping types to functions. Some common solutions are to call reflect.TypeOf() or to use fmt with %T (which also uses reflection). Instead, this package creates empty structs with a generic. This captures the type and can be used for the mappings.

type kind[T any] struct{} // Usable as key for a map.

The downside is that there is not that much that can be done with that. It is not possible to get a string representation, know with which type is being dealt with inside the package, implement methods that deal with the providers or dependencies in the container due to being done through generics.

Usage

Create a new container:

container := di.NewContainer()

Register providers:

provider := func(_ *di.Container) (*SomeStruct, error) {
    return NewSomeStruct(100)
}

di.Bind(container, provider)

Resolve dependencies:

instance, err = di.Make[*SomeStruct](container)

Binding singletons

In this case, the provider will be registered and only called the first time the instance is needed. The following calls will return the same instance.

Register:

provider := func(_ *di.Container) (*SomeStruct, error) {
    return NewSomeStruct(100)
}

di.BindSingleton(container, provider)

Resolve:

instance, err = di.Make[*SomeStruct](container)

Binding named providers

Optionally, the container also supports named dependencies by passing a string. This way, it is possible to have more than provider for the same type:

id := "some_id"
container := di.NewContainer()
provider := func(_ *di.Container) (*SomeStruct, error) {
    return NewSomeStruct(100)
}
di.BindNamed(container, id, provider)

instance, err := di.MakeNamed[*SomeStruct](container, id)

di's People

Contributors

jprogr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.