di

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2025 License: MIT Imports: 4 Imported by: 1

README

generic-di

Go Dependency Injection with Generics

Example

Struct

configuration.go

package main

import "git.apihub24.de/admin/generic-di"

func init() {
	// register the Struct Constructor Function for DI
	di.Injectable(NewConfiguration)
}

type Configuration struct {
	UserName string
}

func NewConfiguration() *Configuration {
	return &Configuration{
		UserName: "Markus",
	}
}

greeter.go

package main

import (
	"fmt"
	"git.apihub24.de/admin/generic-di"
)

func init() {
	di.Injectable(NewGreeter)
}

type Greeter struct {
	config *Configuration
}

func NewGreeter() *Greeter {
	return &Greeter{
		// here was the Configuration from configuration.go injected
		config: di.Inject[*Configuration](),
	}
}

func (ctx *Greeter) Greet() string {
	return fmt.Sprintf("Hello, %s", ctx.config.UserName)
}

message_service.go

package main

import "git.apihub24.de/admin/generic-di"

func init() {
	di.Injectable(NewMessageService)
}

type MessageService struct {
	greeter *Greeter
}

func NewMessageService() *MessageService {
	return &MessageService{
		// here was the Greeter from greeter.go injected
		greeter: di.Inject[*Greeter](),
	}
}

func (ctx *MessageService) Welcome() string {
	return ctx.greeter.Greet()
}

main.go

package main

import di "git.apihub24.de/admin/generic-di"

func main() {
	msgService := di.Inject[*MessageService]()
	// prints the message "Hello, Markus"
	println(msgService.Welcome())
}
Interface

message_service.go

package main

import "git.apihub24.de/admin/generic-di"

func init() {
	di.Injectable(newMessageService)
}

type IMessageService interface {
	Welcome() string
}

type messageService struct {
	greeter *Greeter
}

func NewMessageService() IMessageService {
	return &messageService{
		// here was the Greeter from greeter.go injected
		greeter: di.Inject[*Greeter](),
	}
}

func (ctx *messageService) Welcome() string {
	return ctx.greeter.Greet()
}

main.go

package main

import di "git.apihub24.de/admin/generic-di"

func main() {
	msgService := di.Inject[IMessageService]()
	// prints the message "Hello, Markus"
	println(msgService.Welcome())
}

Replace Instance

services/message_service.go

package services

import "git.apihub24.de/admin/generic-di"

func init() {
	di.Injectable(newMessageService)
}

type IMessageService interface {
	Welcome() string
}

type messageService struct {
	greeter *Greeter
}

func NewMessageService() IMessageService {
	return &messageService{
		// here was the Greeter from greeter.go injected
		greeter: di.Inject[*Greeter](),
	}
}

func (ctx *messageService) Welcome() string {
	return ctx.greeter.Greet()
}

main.go

package main

import di "git.apihub24.de/admin/generic-di"

func main() {
	msgService := di.Inject[services.IMessageService]()
	// prints the message "Hello, Markus"
	println(msgService.Welcome())
}

message_service_test.go

package services_test

func init() {
	// replace the instance of IMessageService with the Mock
	di.Replace(newMessageServiceMock)
}

type messageServiceMock struct {}

func newMessageServiceMock() services.IMessageService {
	return &messageServiceMock{}
}

func (svc *messageServiceMock) Welcome() string {
	return "Hello, Mock"
}

func TestMessageServiceMocking(t *testing.T) {
	service := di.Inject[services.IMessageService]()
	if service.Welcome() != "Hello, Mock" {
		t.Errorf("expect Hello, Mock but get %s", service.Welcome())
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Destroy

func Destroy[T any](identifier ...string)

func DestroyAllMatching added in v1.6.0

func DestroyAllMatching(match func(string) bool)

func Inject

func Inject[T any](identifier ...string) T

Inject gets or create a Instance of the Struct used the Injectable constructor Function

func Injectable

func Injectable[T any](creator func() T)

Injectable marks a constructor Function of a Struct for DI

func Replace added in v1.4.0

func Replace[T any](creator func() T, identifier ...string)

func ReplaceInstance added in v1.5.0

func ReplaceInstance[T any](instance T, identifier ...string)

Types

This section is empty.

Jump to

Keyboard shortcuts

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