Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

owtorg/autoinject

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AutoInject

AutoInject is a lightweight golang dependency injection framework

Build Status: Build Status

Basic Usage - AutoInjection

import "github.com/henrythethird/autoinject"

Given a service you'd like to inject, register it with the fully qualified struct name to the container:

type Name struct {}

container := NewContainer()
container.Register("mypackage.Name", func() interface{} {
    return new(Name)
})

Now we need to set up the receiving container with the necessary tags. Note: Make sure the service is a pointer

type SomeStruct struct {
    Service *Name `autoinject="-"`
}

Once we have done this, we can tell the container to automatically inject the described services

myService := new(SomeStruct)

container.AutoInject(myService)

Basic Usage - Parameter Injection

The injectable named parameters can be of any type. Here a basic example

container := NewContainer()
container.AddParameter("theAnswerToEverything", 42)

// We can simply get the parameter out again:
container.GetParameter("theAnswerToEverything")
/* This requires the parameter to be cast, hence is a bit ugly.
 * Another solution is to create a struct and automatically inject the named parameter:
 */

type SomeStruct struct {
    Answer int `autoinject="theAnswerToEverything"`
}

myStruct := new(SomeStruct)
container.AutoInject(myStruct)
// Now the parameter is injected
Morty Proxy This is a proxified and sanitized view of the page, visit original site.