Code Monkey home page Code Monkey logo

godi's Introduction

godi

Gitter GoDoc Build Status Coverage Go Report Card FOSSA Status

Dependency injection in Golang at runtime.

Installation

Make sure that Go is installed on your computer. Type the following command in your terminal:

go get github.com/asaskevich/godi

or you can get specified release of the package with gopkg.in:

go get gopkg.in/asaskevich/godi.v1.0.0

After it the package is ready to use.

Import package in your project

Add following line in your *.go file:

import "github.com/asaskevich/godi"

Example of usage

To start you need to create structs (like services) or factory functions. Be sure to use tag godi on struct fields that will be used for the dependency injection.

type Repository struct {
	Driver CustomDriver `godi:"autowire"`
    Print Printer `godi:"autowire"`
}

type CustomDriver struct {
	Name string
}

type Printer struct {
    // fields
}
...

c := Container{}
c.New()
c.RegisterService(CustomDriver{Name:"my_driver"})
c.RegisterFactory(func () Printer {
    // init Printer
    return Printer{}
})

service, err := c.ConstructService(Repository{})

println(service.(Repository).Driver.Name) // "my_driver"

List of functions and types

type Container
    func (c *Container) ConstructService(item interface{}) (interface{}, error)
    func (c *Container) GetFactory(factoryType string) (func() interface{}, error)
    func (c *Container) GetService(serviceType string) (interface{}, error)
    func (c *Container) HasFactory(service interface{}) (res bool)
    func (c *Container) HasService(item interface{}) (res bool)
    func (c *Container) New()
    func (c *Container) RegisterFactory(service func() interface{})
    func (c *Container) RegisterService(item interface{})

Tag godi

Note: without the tag inner structures will not be created

  • - - to skip processing field
  • autowire - allows building inner service automatically

Notes

Documentation is available here: godoc.org. Full information about code coverage is also available here: godi on gocover.io.

Support

If you do have a contribution to the package, feel free to create a Pull Request or an Issue.

What to contribute

If you don't know what to do, there are some features and functions that need to be done

  • Refactor code
  • Edit docs and README: spellcheck, grammar and typo check
  • Resolve issues and bugs
  • Look at forks for new features and fixes
  • Dependency injection by sub-type
  • Resolve dependency conflicts (e.g. two types matched one common type)
  • Qualifiers to define custom Id of service
  • Autoscan
  • Separate services into repositores/components/services/controllers
  • Autowire string/int properties from files

License

FOSSA Status

godi's People

Contributors

asaskevich avatar fossabot avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

fossabot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.