dicgo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2021 License: MIT Imports: 1 Imported by: 0

README

DICGO

dicGO is simplest possible DIC (depenency injection container) written after minidic

Install

go get -u github.com/rukavina/dicgo

Usage

import "github.com/rukavina/dicgo"

//create container
dicgo.NewContainer()

type service1 struct {
	id  string
	num int
}

type service2 struct {
	s1   service1
	name string
}

//add singleton service
dicgo.AddSingleton("s1", func(c Container)interface{}{
    return &s1{
        id: "s1",
        num: 1,
    }
})

//add another service, depending on s1 service
dicgo.AddSingleton("s2", func(c Container)interface{}{
    return &s2{
        //watch out!, unlike Get, Service methods panics if service not found
        s1: c.Service("s1").(*service1)
        name: "s2",
    }
})

//add just value
dicgo.AddValue("config", map[string]interface{}{
    "test": 123,
})

//add factory, non sigleton, always returns new instance
dicgo.AddValue("gen", func(c Container)interface{}{
    return &s1{
        id: "new",
        num: 1,
    }
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container interface {
	// SetService new service definition
	SetService(id string, fact Factory, singleton bool)
	// SetSingleton adds new singleton service
	SetSingleton(id string, fact Factory)
	// SetFactory adds new non sigleton service factory
	SetFactory(id string, fact Factory)
	// SetValue adds non-service value
	SetValue(id string, val interface{})
	// Get return service
	Get(id string) (interface{}, bool)
	// Service returns service, or panics if not defined
	Service(id string) interface{}
	// Raw returns raw service factory
	Raw(id string) (func(c Container) interface{}, bool)
	// Has returns if container has service definition
	Has(id string) bool
	// Del deletes service from the container
	Del(id string)
}

Container is main DIC interface

func NewContainer

func NewContainer() Container

NewContainer creates and returns new DIC

type Factory

type Factory func(c Container) interface{}

Factory service "constructor"

Jump to

Keyboard shortcuts

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