forked from irahardianto/service-pattern-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servicecontainer.go
44 lines (34 loc) · 1.04 KB
/
servicecontainer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"sync"
"github.com/irahardianto/service-pattern-go/controllers"
"github.com/irahardianto/service-pattern-go/repositories"
"github.com/irahardianto/service-pattern-go/infrastructures"
"github.com/irahardianto/service-pattern-go/services"
"database/sql"
)
type IServiceContainer interface {
InjectPlayerController() controllers.PlayerController
}
type kernel struct{}
func (k *kernel) InjectPlayerController() controllers.PlayerController {
sqlConn, _ := sql.Open("sqlite3", "/var/tmp/tennis.db")
sqliteHandler := &infrastructures.SQLiteHandler{}
sqliteHandler.Conn = sqlConn
playerRepository := &repositories.PlayerRepository{sqliteHandler}
playerService := &services.PlayerService{&repositories.PlayerRepositoryWithCircuitBreaker{playerRepository}}
playerController := controllers.PlayerController{playerService}
return playerController
}
var (
k *kernel
containerOnce sync.Once
)
func ServiceContainer() IServiceContainer {
if k == nil {
containerOnce.Do(func() {
k = &kernel{}
})
}
return k
}