Skip to content

actforgood/xdi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Xdi

Build Status License Coverage Status Goreportcard Go Reference


Package xdi provides a centralized dependency injection manager which holds definitions for an application's objects/dependencies.

Installation

$ go get github.com/actforgood/xdi

Example

Basic example:

// DiManager holds application's objects, dependencies.
// Do not inject it/use it directly, in your application's objects.
// It should be used only in the bootstrap process of your application and/or main.go,
// as a centralized container of dependencies.
// Note: instead of declaring a variable, you can also use the singleton provided by xdi.ManagerInstance().
var DiManager = xdi.NewManager()

func init() {
	DiManager.AddDefinition(xdi.Definition{
		ID: "app.repository.product",
		Initializer: func() any {
			return NewDummyProductRepository()
		},
		Shared: true,
	})
}

func init() {
	DiManager.AddDefinition(xdi.Definition{
		ID: "app.service.product",
		Initializer: func() any {
			return NewDummyProductService(
				DiManager.Get("app.repository.product").(ProductRepository),
			)
		},
		Shared: true,
	})
}

func main() {
	productService := DiManager.Get("app.service.product").(ProductService)
	isAvailable, _ := productService.CheckAvailability("some-sku", 2)
	fmt.Println("isAvailable:", isAvailable)
}

Misc

Feel free to use this pkg if you like it and fits your needs.
As it is a light/lite pkg, you can also just copy-paste the code, instead of importing it, keeping the license header.

License

This package is released under a MIT license. See LICENSE.

About

Golang Dependency Injection Package

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Contributors