gs

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2021 License: MIT Imports: 1 Imported by: 0

README

Go Service Container

Go Report Card

go-services library implements Service Container pattern. It creates instances of objects, resolving their dependencies using simple API.

To resolve services dependencies simply add a public Pointer type field in your service structure or declare method GoService which arguments is pointers to other services.

Usage example

Declaring db package

package db

type Service struct {}

func (svc *Service) GetName() string {
    return "Name"
}

Declaring accounts package

package accounts

import "app/db"

type Account struct {
    Id   uint32
    Name string 
}

type Service struct {
    DBService *db.Service
}

func (svc *Service) GetById(id uint32) *Account {
    return &Account{Id: id, Name: svc.DBService.GetName()}
}

Main application package

package main

import (
    "app/accounts"
    "fmt"
    "github.com/apoprotsky/go-services"
)

func main() {
    accountsService := gs.Get((*accounts.Service)(nil)).(*accounts.Service)
    account := accountsService.GetById(1)
    fmt.Printf("%+v\n", account)
}

See example directory for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(i interface{}) interface{}

Create creates new instance of specified type

func Get

func Get(i interface{}) interface{}

Get returns existing or creates new instance of specified type

func Set

func Set(i interface{}, v interface{})

Set value of instance of specified type

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container struct

func New

func New() *Container

New creates instance of services container

func (*Container) Create

func (c *Container) Create(i interface{}) interface{}

Create creates new instance of specified type

func (*Container) Get

func (c *Container) Get(i interface{}) interface{}

Get returns existing or creates new instance of specified type

func (*Container) Set

func (c *Container) Set(i interface{}, v interface{})

Set value of instance of specified type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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