This library is a reflection based tiny DI container. It was inspired by the interface of the Google Guice.
- Go 1.7+
This package can be installed with the go get command:
$ go get github.com/vvatanabe/shot
package main
import (
"github.com/vvatanabe/shot/shot"
)
func main() {
injector, err := shot.CreateInjector(func(binder shot.Binder) {
store := NewStoreOnMemory()
binder.Bind(new(Store)).ToInstance(store)
binder.Bind(new(GroupRepository)).ToConstructor(NewGroupRepositoryOnMemory)
binder.Bind(new(UserRepository)).To(new(UserRepositoryOnMemory)).In(shot.SingletonInstance)
binder.Bind(new(ProjectService)).AsEagerSingleton()
})
if err != nil {
panic(err)
}
userRepository := injector.Get(new(UserRepository)).(UserRepository)
groupRepository := injector.Get(new(GroupRepository)).(GroupRepository)
projectService := injector.Get(new(ProjectService)).(*ProjectService)
}
func NewProjectService(userRepository UserRepository, groupRepository GroupRepository) *ProjectService {
return &ProjectService{
userRepository, groupRepository,
}
}
type ProjectService struct {
UserRepository UserRepository "inject"
GroupRepository GroupRepository "inject"
}
func (u *ProjectService) FindUser() []string {
return u.UserRepository.FindAll()
}
func (u *ProjectService) FindGroup() []string {
return u.GroupRepository.FindAll()
}
func NewStoreOnMemory() *StoreOnMemory {
return &StoreOnMemory{
[]string{"user-1", "user-2", "user-3"},
[]string{"group-1", "group-2", "group-3"},
}
}
type Store interface {
GetUsers() []string
GetGroups() []string
}
type StoreOnMemory struct {
users []string
groups []string
}
func (s *StoreOnMemory) GetUsers() []string {
return s.users
}
func (s *StoreOnMemory) GetGroups() []string {
return s.groups
}
type UserRepository interface {
FindAll() []string
}
func NewUserRepositoryOnMemory(store Store) *UserRepositoryOnMemory {
return &UserRepositoryOnMemory{store}
}
type UserRepositoryOnMemory struct {
Store Store "inject"
}
func (repository *UserRepositoryOnMemory) FindAll() []string {
return repository.Store.GetUsers()
}
type GroupRepository interface {
FindAll() []string
}
func NewGroupRepositoryOnMemory(store Store) *GroupRepositoryOnMemory {
return &GroupRepositoryOnMemory{store}
}
type GroupRepositoryOnMemory struct {
Store Store "inject"
}
func (repository *GroupRepositoryOnMemory) FindAll() []string {
return repository.Store.GetGroups()
}
binder.Bind(new(UserRepository)).To(new(UserRepositoryOnMemory))
binder.Bind(new(UserRepository)).ToConstructor(NewUserRepositoryOnMemory)
binder.Bind(new(ProjectService)).ToInstance(store)
binder.Bind(new(ProjectService)).In(shot.NoScope)
binder.Bind(new(UserRepository)).To(new(UserRepositoryOnMemory)).In(shot.SingletonInstance)
binder.Bind(new(UserRepository)).To(new(UserRepositoryOnMemory)).AsEagerSingleton()
google/guice really inspired me. I appreciate it.
For bugs, questions and discussions please use the Github Issues.
Apache License 2.0