An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 9.0% on proxy.golang.org

proxy.golang.org : github.com/eluv-io/inject-go

Package inject is guice-inspired dependency injection for Go. https://github.com/google/guice/wiki/Motivation This project is in no way affiliated with the Guice project, but I recommend reading their docs to better understand the concepts. A Module is analogous to Guice's AbstractModule, used for setting up your dependencies. This allows you to bind structs, struct pointers, interfaces, and primitives to singletons, constructors, with or without tags. An interface can have a binding to another type, or to a singleton or constructor. An interface can also be bound to a singleton or constructor. A struct, struct pointer, or primitive must have a direct binding to a singleton or constructor. All errors from binding will be returned as one error when calling inject.NewInjector(...). An Injector is analogous to Guice's Injector, providing your dependencies. Given the binding: We are able to get a value for SayHello. See the Injector interface for other methods. A constructor is a function that takes injected values as parameters, and returns a value and an error. We can set up a constructor to take zero values, or values that require a binding in some module passed to NewInjector(). A singleton constructor will be called exactly once for the entire application. The simplest way of binding an interface to a constructor function is to use the `BindConstructor` or `BindSingletonConstructor` methods. They automatically determine the interface type that is bound to the constructor from the constructor's return value. The following examples are equivalent to the more verbose ones above: A singleton bound through a constructor function can be marked as _eager_, in which case it will be constructed automatically by the injector during the injector creation process. The advantage is that every time the singleton is injected it is already available, whereas a normal (_lazy_) singleton has to be created before injecting it the first time. Eager singletons also reveal initialization problems sooner - at the time of injector creation rather than the first time the singleton is used. In addition, an eager singleton can be combined with an additional arbitrary function call, that will also be performed by the injector on construction. This can be used, for example, to initialize a "traditional" singleton implemented with a global variable: Functions can be called from an injector using the Call function. These functions have the same parameter requirements as constructors, but can have any return types. See the methods on Module and Constructor for more details. A tag allows named multiple bindings of one type. As an example, let's consider if we want to have multiple ways to say hello. Structs can also be populated using the tag "inject". Constructors can be tagged using structs, either named or anonymous. A constructor can mix tagged values with untagged values in the input struct. The CallTagged function works similarly to Call, except can take parameters like a tagged constructor. A child injector is built from an existing injector (it's parent). It inherits all bindings and singletons of its parent injector and can add its own additional bindings. However, it is not allowed to redefine bindings that already exist in the parent injector. Child injectors allow building injector hierarchies that resolve the problem where a given interface has multiple implementations and dependent components require one or the other implementation depending on some runtime condition that is not available at creation time of the (parent) injector. See this discussion on hierarchical injectors for further information and possible alternatives using factories: https://publicobject.com/2008/06/whats-hierarchical-injector.html Both Module and Injector implement fmt.Stringer for inspection, however this may be added to in the future to allow semantic inspection of bindings. For testing, production modules may be overridden with test bindings as follows:

Registry - Source - Documentation - JSON
purl: pkg:golang/github.com/eluv-io/inject-go
License: MIT
Latest release: about 2 years ago
First release: over 3 years ago
Namespace: github.com/eluv-io
Stars: 0 on GitHub
Forks: 0 on GitHub
See more repository details: repos.ecosyste.ms
Last synced: 10 days ago

    Loading...
    Readme
    Loading...