di

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 13, 2023 License: CC0-1.0 Imports: 0 Imported by: 0

README

Compile Time Dependancy Injection for Golang

This library generates constructor functions with dependancy code for struct and interfaces.

The library is inspired by google wire

Getting Started

Download and install using following commands

go get github.com/siddhesh-tamhanekar/di@v0.0.3

go install github.com/siddhesh-tamhanekar/di/cmd/di

Execute following command for generating dependancies in your project.

<goroot>/bin/di

Running Example from source code

go run .


How to use the Library
  • Create file called di.go (this file can be declared in every package where as per your need)
  • go:build exclude to ignore di.go while compiling project.
  • This file will contain the code which tells library about how the dependancies should be resolved.
  • we can use the library methods (mentioned below) to declare the ependancies.
  • Once di.go is ready we can run <goroot>/bin/di.go to generate dependancies.
  • Refer example directory for more details.
Methods
Function Usage
Share di.Share(yourStruct{},db)
the second parameter is the package level variable which need to use while resolving dependancy it will act like a signleton
Build di.Build(yourStruct{})
build method will create constructor function for given struct
Bind di.Bind(yourInterface, targetStruct{}
this will bind Interface to struct
BindEnv di.Bind(yourInterface, targetStruct{},env)
this will bind Interface to struct when ENV environment variable is set to env string
Conventions

All the functions generated with Build method call will start with New keyword followed by struct name for e.g. generated function for UserRepository struct will be NewUserRepository() UserRepository

All the functions genertated with Bind method call will start with New keyword followed by interface name for e.g. generated function for UserCreator interface will be New UserCreator() UserCreator

Closing Thoughts

This library is still in beta and needs to handle the edge case scenerios. pls feel free to open issues and pull request to enrich the library.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(src any, dest any)

func BindEnv

func BindEnv(src any, dest any, env string)

func Build

func Build(src any)

func Share

func Share(src any, code any)

Types

This section is empty.

Directories

Path Synopsis
cmd
di

Jump to

Keyboard shortcuts

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