Simplified DI resolution

This commit is contained in:
Deluan 2016-03-11 15:16:17 -05:00
parent e9861e1b26
commit f4c1dbdd3c
8 changed files with 12 additions and 22 deletions

View file

@ -7,7 +7,6 @@ import (
"github.com/deluan/gosonic/api/responses" "github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/engine" "github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils" "github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
) )
type BrowsingController struct { type BrowsingController struct {
@ -16,7 +15,7 @@ type BrowsingController struct {
} }
func (c *BrowsingController) Prepare() { func (c *BrowsingController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.browser) utils.ResolveDependencies(&c.browser)
} }
func (c *BrowsingController) GetMediaFolders() { func (c *BrowsingController) GetMediaFolders() {

View file

@ -8,7 +8,6 @@ import (
"github.com/deluan/gosonic/domain" "github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/engine" "github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils" "github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
) )
type GetAlbumListController struct { type GetAlbumListController struct {
@ -20,7 +19,7 @@ type GetAlbumListController struct {
type strategy func(offset int, size int) (*domain.Albums, error) type strategy func(offset int, size int) (*domain.Albums, error)
func (c *GetAlbumListController) Prepare() { func (c *GetAlbumListController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.listGen) utils.ResolveDependencies(&c.listGen)
c.types = map[string]strategy{ c.types = map[string]strategy{
"random": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetRandom(o, s) }, "random": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetRandom(o, s) },

View file

@ -5,7 +5,6 @@ import (
"github.com/deluan/gosonic/api/responses" "github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/engine" "github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils" "github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
) )
type GetCoverArtController struct { type GetCoverArtController struct {
@ -14,7 +13,7 @@ type GetCoverArtController struct {
} }
func (c *GetCoverArtController) Prepare() { func (c *GetCoverArtController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.cover) utils.ResolveDependencies(&c.cover)
} }
func (c *GetCoverArtController) Get() { func (c *GetCoverArtController) Get() {

View file

@ -5,7 +5,6 @@ import (
"github.com/deluan/gosonic/api/responses" "github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/engine" "github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils" "github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
) )
type PlaylistsController struct { type PlaylistsController struct {
@ -14,7 +13,7 @@ type PlaylistsController struct {
} }
func (c *PlaylistsController) Prepare() { func (c *PlaylistsController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.pls) utils.ResolveDependencies(&c.pls)
} }
func (c *PlaylistsController) GetAll() { func (c *PlaylistsController) GetAll() {

View file

@ -5,7 +5,6 @@ import (
"github.com/deluan/gosonic/api/responses" "github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/engine" "github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils" "github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
) )
type SearchingController struct { type SearchingController struct {
@ -14,7 +13,7 @@ type SearchingController struct {
} }
func (c *SearchingController) Prepare() { func (c *SearchingController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.search) utils.ResolveDependencies(&c.search)
} }
func (c *SearchingController) Search2() { func (c *SearchingController) Search2() {

View file

@ -6,7 +6,6 @@ import (
"github.com/deluan/gosonic/domain" "github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/engine" "github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils" "github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
) )
type StreamController struct { type StreamController struct {
@ -17,7 +16,7 @@ type StreamController struct {
} }
func (c *StreamController) Prepare() { func (c *StreamController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.repo) utils.ResolveDependencies(&c.repo)
c.id = c.RequiredParamString("id", "id parameter required") c.id = c.RequiredParamString("id", "id parameter required")

View file

@ -27,14 +27,8 @@ func StartImport() {
go func() { go func() {
// TODO Move all to DI // TODO Move all to DI
i := &Importer{mediaFolder: beego.AppConfig.String("musicFolder")} i := &Importer{mediaFolder: beego.AppConfig.String("musicFolder")}
utils.ResolveDependency(&i.mfRepo) utils.ResolveDependencies(&i.mfRepo, &i.albumRepo, &i.artistRepo, &i.idxRepo, &i.plsRepo,
utils.ResolveDependency(&i.albumRepo) &i.propertyRepo, &i.search, &i.scanner)
utils.ResolveDependency(&i.artistRepo)
utils.ResolveDependency(&i.idxRepo)
utils.ResolveDependency(&i.plsRepo)
utils.ResolveDependency(&i.propertyRepo)
utils.ResolveDependency(&i.search)
utils.ResolveDependency(&i.scanner)
i.Run() i.Run()
}() }()
} }

View file

@ -25,8 +25,10 @@ func DefineSingleton(ptr interface{}, constructor interface{}) interface{} {
return ptr return ptr
} }
func ResolveDependency(ptr interface{}) { func ResolveDependencies(ptrs ...interface{}) {
inject.ExtractAssignable(Graph, ptr) for _, p := range ptrs {
inject.ExtractAssignable(Graph, p)
}
} }
func init() { func init() {