Fixed problem with DI

This commit is contained in:
Deluan 2016-03-08 18:33:35 -05:00
parent e10e730af1
commit 067517a916
3 changed files with 15 additions and 11 deletions

View file

@ -9,13 +9,13 @@ import (
func init() {
// Persistence
ir := utils.DefineSingleton(new(domain.ArtistIndexRepository), persistence.NewArtistIndexRepository)
pr := utils.DefineSingleton(new(domain.PropertyRepository), persistence.NewPropertyRepository)
fr := utils.DefineSingleton(new(domain.MediaFolderRepository), persistence.NewMediaFolderRepository)
ar := utils.DefineSingleton(new(domain.ArtistRepository), persistence.NewArtistRepository)
alr := utils.DefineSingleton(new(domain.AlbumRepository), persistence.NewAlbumRepository)
mr := utils.DefineSingleton(new(domain.MediaFileRepository), persistence.NewMediaFileRepository)
utils.DefineSingleton(new(domain.ArtistIndexRepository), persistence.NewArtistIndexRepository)
utils.DefineSingleton(new(domain.PropertyRepository), persistence.NewPropertyRepository)
utils.DefineSingleton(new(domain.MediaFolderRepository), persistence.NewMediaFolderRepository)
utils.DefineSingleton(new(domain.ArtistRepository), persistence.NewArtistRepository)
utils.DefineSingleton(new(domain.AlbumRepository), persistence.NewAlbumRepository)
utils.DefineSingleton(new(domain.MediaFileRepository), persistence.NewMediaFileRepository)
// Engine (Use cases)
utils.DefineSingleton(new(engine.Browser), engine.NewBrowser, pr, fr, ir, ar, alr, mr)
utils.DefineSingleton(new(engine.Browser), engine.NewBrowser)
}

View file

@ -1,11 +1,13 @@
package tests
import (
"github.com/astaxie/beego"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/astaxie/beego"
"github.com/deluan/gosonic/utils"
)
func Init(t *testing.T, skipOnShort bool) {
@ -21,4 +23,5 @@ func Init(t *testing.T, skipOnShort bool) {
if noLog != "" {
beego.SetLevel(beego.LevelError)
}
utils.Graph.Finalize()
}

View file

@ -1,8 +1,9 @@
package utils
import (
"github.com/karlkfi/inject"
"reflect"
"github.com/karlkfi/inject"
)
var Graph inject.Graph
@ -11,9 +12,9 @@ var (
definitions map[reflect.Type]interface{}
)
func DefineSingleton(ptr interface{}, constructor interface{}, args ...interface{}) interface{} {
func DefineSingleton(ptr interface{}, constructor interface{}) interface{} {
typ := reflect.TypeOf(ptr)
provider := inject.NewProvider(constructor, args...)
provider := inject.NewAutoProvider(constructor)
if _, found := definitions[typ]; found {
ptr = definitions[typ]