Skip to content

Go Services container

License

Notifications You must be signed in to change notification settings

apoprotsky/go-services

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.