-
Notifications
You must be signed in to change notification settings - Fork 0
/
servicecollection.go
81 lines (67 loc) · 2.48 KB
/
servicecollection.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package dependencyinjection
import (
"github.com/yoyofxteam/reflectx"
"strings"
)
type ServiceCollection struct {
serviceDescriptors []*ServiceDescriptor
serviceDescriptorMaps map[string]int
}
func NewServiceCollection() *ServiceCollection {
return &ServiceCollection{serviceDescriptorMaps: make(map[string]int)}
}
//Singleton
//Scoped
//Transient
func (sc *ServiceCollection) AddServiceDescriptor(sd *ServiceDescriptor) {
typeName := sd.Name
if typeName == "" {
typeName, _ = reflectx.GetCtorFuncOutTypeName(sd.Provider)
typeName = strings.ToLower(typeName)
}
index := len(sc.serviceDescriptors)
defIndex, exist := sc.serviceDescriptorMaps[typeName]
if exist {
sc.serviceDescriptors[defIndex] = sd
} else {
sc.serviceDescriptorMaps[typeName] = index
sc.serviceDescriptors = append(sc.serviceDescriptors, sd)
}
}
func (sc *ServiceCollection) AddSingleton(provider interface{}) {
sd := NewServiceDescriptorByProvider(provider, Singleton)
sc.AddServiceDescriptor(sd)
}
func (sc *ServiceCollection) AddSingletonAndName(name string, provider interface{}) {
sc.AddSingletonByName(name, provider)
sc.AddSingleton(provider)
}
func (sc *ServiceCollection) AddSingletonByName(name string, provider interface{}) {
sd := NewServiceDescriptorByName(name, provider, Singleton)
sc.AddServiceDescriptor(sd)
}
func (sc *ServiceCollection) AddSingletonByImplementsAndName(name string, provider interface{}, implements interface{}) {
sc.AddSingletonByName(name, provider)
sc.AddSingletonByImplements(provider, implements)
}
func (sc *ServiceCollection) AddSingletonByImplements(provider interface{}, implements interface{}) {
sd := NewServiceDescriptorByImplements(provider, implements, Singleton)
sc.AddServiceDescriptor(sd)
}
func (sc *ServiceCollection) AddSingletonByNameAndImplements(name string, provider interface{}, implements interface{}) {
sd := NewServiceDescriptorByImplements(provider, implements, Singleton)
sd.Name = name
sc.AddServiceDescriptor(sd)
}
func (sc *ServiceCollection) AddTransient(provider interface{}) {
sd := NewServiceDescriptorByProvider(provider, Transient)
sc.AddServiceDescriptor(sd)
}
func (sc *ServiceCollection) AddTransientByName(name string, provider interface{}) {
sd := NewServiceDescriptorByName(name, provider, Transient)
sc.AddServiceDescriptor(sd)
}
func (sc *ServiceCollection) AddTransientByImplements(provider interface{}, implements interface{}) {
sd := NewServiceDescriptorByImplements(provider, implements, Transient)
sc.AddServiceDescriptor(sd)
}